-
Notifications
You must be signed in to change notification settings - Fork 998
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add base Sentry client, and stack trace parser
- Loading branch information
1 parent
2de3f87
commit c79e86c
Showing
11 changed files
with
318 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...StripeFinancialConnections/Source/Analytics/Sentry/FinancialConnectionsSentryClient.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// FinancialConnectionsSentryClient.swift | ||
// StripeFinancialConnections | ||
// | ||
// Created by Mat Schmid on 2024-10-22. | ||
// | ||
|
||
import Foundation | ||
@_spi(STP) import StripeCore | ||
|
||
protocol FinancialConnectionsErrorReporter { | ||
func report(error: Error, parameters: [String: Any]) | ||
} | ||
|
||
class FinancialConnectionsSentryClient: FinancialConnectionsErrorReporter { | ||
private static let endpoint: URL = { | ||
let projectId = "871" | ||
var components = URLComponents() | ||
components.scheme = "https" | ||
components.host = "errors.stripe.com" | ||
components.path = "/api/\(projectId)/envelope/" | ||
return components.url! | ||
}() | ||
|
||
func report(error: Error, parameters: [String: Any]) { | ||
// TODO | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...nancialConnections/StripeFinancialConnections/Source/Analytics/Sentry/SentryContext.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// SentryContext.swift | ||
// StripeFinancialConnections | ||
// | ||
// Created by Mat Schmid on 2024-10-22. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
@_spi(STP) import StripeCore | ||
|
||
struct SentryContext: Encodable { | ||
let app: SentryAppContext | ||
let os: SentryOsContext | ||
let device: SentryDeviceContext | ||
|
||
static let shared: SentryContext = { | ||
let app = SentryAppContext( | ||
appIdentifier: Bundle.stp_applicationBundleId() ?? "", | ||
appName: Bundle.stp_applicationName() ?? "", | ||
appVersion: Bundle.stp_applicationVersion() ?? "" | ||
) | ||
let os = SentryOsContext( | ||
name: "iOS", | ||
version: UIDevice.current.systemVersion, | ||
type: InstallMethod.current.rawValue | ||
) | ||
let device = SentryDeviceContext( | ||
modelId: UIDevice.current.identifierForVendor?.uuidString ?? "", | ||
model: UIDevice.current.model, | ||
manufacturer: "Apple", | ||
type: STPDeviceUtils.deviceType ?? "" | ||
) | ||
|
||
return SentryContext(app: app, os: os, device: device) | ||
}() | ||
} | ||
|
||
struct SentryAppContext: Encodable { | ||
let appIdentifier: String | ||
let appName: String | ||
let appVersion: String | ||
} | ||
|
||
struct SentryOsContext: Encodable { | ||
let name: String | ||
let version: String | ||
let type: String | ||
} | ||
|
||
struct SentryDeviceContext: Encodable { | ||
let modelId: String | ||
let model: String | ||
let manufacturer: String | ||
let type: String | ||
} |
16 changes: 16 additions & 0 deletions
16
...nancialConnections/StripeFinancialConnections/Source/Analytics/Sentry/SentryPayload.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// SentryPayload.swift | ||
// StripeFinancialConnections | ||
// | ||
// Created by Mat Schmid on 2024-10-22. | ||
// | ||
|
||
import Foundation | ||
@_spi(STP) import StripeCore | ||
|
||
struct SentryPayload: Encodable { | ||
let eventId: String = UUID().uuidString.replacingOccurrences(of: "-", with: "") | ||
let timestamp: TimeInterval = Date().timeIntervalSince1970 | ||
let release: String = StripeAPIConfiguration.STPSDKVersion | ||
let context: SentryContext = .shared | ||
} |
47 changes: 47 additions & 0 deletions
47
...cialConnections/StripeFinancialConnections/Source/Analytics/Sentry/SentryStacktrace.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// SentryStacktrace.swift | ||
// StripeFinancialConnections | ||
// | ||
// Created by Mat Schmid on 2024-10-22. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol StackTrace: Encodable, Equatable { | ||
var function: String { get } | ||
} | ||
|
||
struct RootStackTrance: StackTrace { | ||
let file: String | ||
let function: String | ||
let line: Int | ||
} | ||
|
||
struct CallStackTrace: StackTrace { | ||
let module: String | ||
let function: String | ||
} | ||
|
||
enum SentryStacktrace { | ||
static func capture( | ||
filePath: String = #file, | ||
function: String = #function, | ||
line: Int = #line, | ||
callsiteDepth: Int = 1 | ||
) -> [any StackTrace] { | ||
var traces: [any StackTrace] = [] | ||
if let file = filePath.components(separatedBy: "/").last { | ||
traces.append(RootStackTrance( | ||
file: file, | ||
function: function, | ||
line: line | ||
)) | ||
} | ||
|
||
// Add 1 to the callsite depth to remove the meta call to `SentryStacktrace.capture` | ||
// from the stacktrace. | ||
let callStackTrace = TraceSymbolsParser.current(callsiteDepth: callsiteDepth + 1) | ||
traces.append(contentsOf: callStackTrace) | ||
return traces | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...alConnections/StripeFinancialConnections/Source/Analytics/Sentry/TraceSymbolsParser.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// TraceSymbolsParser.swift | ||
// StripeFinancialConnections | ||
// | ||
// Created by Mat Schmid on 2024-10-22. | ||
// | ||
|
||
import Foundation | ||
|
||
enum TraceSymbolsParser { | ||
/// Get the current call stack as an `CallStackTrace` array representaion. | ||
/// Removes the first `callsiteDepth` traces from the stack. | ||
/// These traces are usually meta traces from classes calling the parser. | ||
/// i.e. removes `TraceSymbolsParser.current` from the stack trace | ||
static func current(callsiteDepth: Int = 0) -> [CallStackTrace] { | ||
let callStackSymbols: [String] = Array(Thread.callStackSymbols.dropFirst(callsiteDepth)) | ||
return callStackSymbols.compactMap { symbols in | ||
Self.parse(symbols: symbols) | ||
} | ||
} | ||
|
||
/// Parses a line of `Thread.callStackSymbols` to `CallStackTrace`. | ||
/// - `symbols`: Input which follows the `DLADDR` format. | ||
/// ``` | ||
/// // {depth} {fname} {fbase} {sname} + {saddr} | ||
/// (number with radix 10) (string) (number with radix 16) (string) + (number with radix 10) | ||
/// ``` | ||
/// This extracts `fname` into the `module`, and `sname` into the `functions.` | ||
static func parse(symbols: String) -> CallStackTrace? { | ||
// Split the input string by whitespaces and filter out empty components | ||
let components = symbols.split(whereSeparator: \.isWhitespace).filter { !$0.isEmpty } | ||
guard components.indices.contains(3) else { | ||
return nil // Invalid symbol, not enough components. | ||
} | ||
// The `module` is the second component | ||
let module = String(components[1]) | ||
|
||
// The `function` is everything from the fourth component up to but not including the "+" | ||
let functionComponents = components[3...] | ||
let functionString = functionComponents.joined(separator: " ") | ||
|
||
// Find the index of the "+" symbol in the original string | ||
guard let plusIndex = functionString.range(of: "+")?.lowerBound else { | ||
return nil // "+" symbol not found. | ||
} | ||
|
||
// Extract the final function string from the joined components, trimmed and until the "+" | ||
let functionEndIndex = symbols.distance(from: symbols.startIndex, to: plusIndex) | ||
let function = String(functionString.prefix(functionEndIndex)).trimmingCharacters(in: .whitespaces) | ||
return CallStackTrace(module: module, function: function) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.