import CottonKit import Foundation import Sentry /// The Bugsink half of `CottonKit/Reporting.swift` — the ONLY file that /// imports Sentry, and the Mac's `Reporting+Sentry.swift` with the phone's /// differences written in. /// /// Everything decided is decided a target down, in `reportingPlan`, where a /// test can reach it. This file is the wiring: start the SDK if the plan says /// to, install the sink, and hang an uncaught-exception handler off the same /// switch. /// /// **What is deliberately NOT installed: the crash handler, and every /// automatic instrument.** Bugsink stores a dSYM and never reads one /// (bugsink/bugsink#20), so SentryCrash's signal and mach handlers would file a /// wall of hex addresses and take the crash away from Apple, whose /// `betaFeedbackCrashSubmissions` arrive symbolicated — `pnpm asc:crashes` is /// the crash instrument, and the shipping lane keeps the archive's dSYMs for /// `atos` (the fastlane lane's `preserve_dsyms!`). Swizzling is off with it: /// the SDK's network breadcrumbs would hook `URLSession`, which this app never /// uses — every byte to a Cotton server goes through Rust's signed transport /// (`SourceGuardTests`) — and its screen and hang tracking would file events /// about the person's screens that nobody asked for. What Bugsink gets is what /// `Reporting.report` sends: a sentence, the four facts, and the log ring. enum BugsinkReporting { /// Start reporting if this build reports, and say what was decided. /// /// Returns the plan so the caller can put its `reason` in the log — a build /// that is silent should be able to say why it is silent. @discardableResult static func start(_ facts: BuildFacts = .fromBundle()) -> ReportingPlan { #if DEBUG let plan = reportingPlan(facts, debugBuild: true) #else let plan = reportingPlan(facts, debugBuild: false) #endif guard plan.sends else { return plan } SentrySDK.start { options in options.dsn = plan.dsn // Build-variant truth, from the bundle the release lane stamped — // with `-dev` on Cotton Dev, which shares this Bugsink project. options.environment = plan.environment options.releaseName = plan.release // See the type comment: the crash reporter is Apple's here. options.enableCrashHandler = false // A thread stack with no symbols is noise on top of the sentence // that is the actual payload. Rust's panics arrive already // symbolic, in the message. options.attachStacktrace = false options.sendDefaultPii = false options.enableAutoSessionTracking = false options.enableAutoPerformanceTracing = false // No hooks into the app: no URLSession swizzle, no view-controller // breadcrumbs, no hang or watchdog events, no screenshots. options.enableSwizzling = false options.enableNetworkTracking = false options.enableNetworkBreadcrumbs = false options.enableAutoBreadcrumbTracking = false options.enableAppHangTracking = false options.enableWatchdogTerminationTracking = false options.attachScreenshot = false options.attachViewHierarchy = false } SentrySDK.configureScope { scope in // `app` names the surface; the Mac reports as `cotton-mac`. scope.setTag(value: "cotton-ios", key: "app") // `real` or `dev`: one project serves both apps (prod item 17). scope.setTag(value: plan.flavor.rawValue, key: ReportingPlan.flavorTag) } Reporting.install(SentrySink()) // An exception nobody caught. On iOS an uncaught NSException ends the // process either way; this runs first, so the sentence and the ring // leave before it does. The handler is a C function pointer and can // capture nothing — everything it reaches is a static. NSSetUncaughtExceptionHandler { exception in Reporting.report( Report( kind: .uncaught, message: "\(exception.name.rawValue): \(exception.reason ?? "no reason given")", context: ["thread": Thread.isMainThread ? "main" : "background"] ) ) // The process is going; give the envelope two seconds to leave. SentrySDK.flush(timeout: 2) } return plan } } /// One report, as one Bugsink message: the facts as tags, the ring as an extra. private struct SentrySink: ReportSink { func send(_ report: Report) { SentrySDK.capture(message: report.message) { scope in scope.setTag(value: report.kind.rawValue, key: "cotton_report") for (key, value) in report.context { scope.setTag(value: value, key: key) } // A tag is capped at 200 characters; two hundred lines are not a // tag. An extra is shown whole on the event. scope.setExtra(value: report.log.joined(separator: "\n"), key: "log") } } }