|
| 1 | +/* |
| 2 | + Modified MIT License |
| 3 | + |
| 4 | + Copyright 2026 OneSignal |
| 5 | + |
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights |
| 9 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + copies of the Software, and to permit persons to whom the Software is |
| 11 | + furnished to do so, subject to the following conditions: |
| 12 | + |
| 13 | + 1. The above copyright notice and this permission notice shall be included in |
| 14 | + all copies or substantial portions of the Software. |
| 15 | + |
| 16 | + 2. All copies of substantial portions of the Software may only be used in connection |
| 17 | + with services provided by OneSignal. |
| 18 | + |
| 19 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +import Foundation |
| 29 | +import OneSignalCore |
| 30 | +@_implementationOnly import OneSignalKMP |
| 31 | + |
| 32 | +/// Mirrors the KMP `ObservabilityEvent` catalog so a call site can name an event without seeing KMP |
| 33 | +/// types, which this module imports implementation-only. |
| 34 | +@_spi(OneSignalInternal) |
| 35 | +public enum OSObservabilityEvent: CaseIterable { |
| 36 | + /// Temporary: comes out once its usage question is answered. |
| 37 | + case deviceGesture |
| 38 | + |
| 39 | + /// The `event.name` of the KMP entry, so a test can check the mirror against the catalog. |
| 40 | + var eventName: String { |
| 41 | + kmpEvent.eventName |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// The producer-facing contract, so a call site can take a spy in tests. |
| 46 | +@_spi(OneSignalInternal) |
| 47 | +public protocol OSObservabilityEventRecorderProtocol: AnyObject { |
| 48 | + /// Never throws or blocks. Drops when the event's flag is off or the per-process cap is |
| 49 | + /// reached; queues, bounded, until remote telemetry is attached. |
| 50 | + func record(event: OSObservabilityEvent, attributes: [String: String]) |
| 51 | +} |
| 52 | + |
| 53 | +/// The attach side, driven by `OSRemoteLogger`. Internal because the telemetry is a KMP type. |
| 54 | +protocol OSObservabilityEventRecorderAttaching: AnyObject { |
| 55 | + func attach(_ telemetry: ILogTelemetry) |
| 56 | + |
| 57 | + /// Ignored unless `telemetry` is the attached one, so a logger that lost the install race |
| 58 | + /// cannot detach the winner. |
| 59 | + func detach(_ telemetry: ILogTelemetry) |
| 60 | +} |
| 61 | + |
| 62 | +/// Wraps the shared KMP recorder, which owns the flag check, the pre-attach queue and the |
| 63 | +/// per-process cap. Events ride the remote logger's telemetry, so they share the crash gate |
| 64 | +/// rather than the severity filter. |
| 65 | +@_spi(OneSignalInternal) |
| 66 | +public final class OSObservabilityEventRecorder: OSObservabilityEventRecorderProtocol, OSObservabilityEventRecorderAttaching { |
| 67 | + public static let shared = OSObservabilityEventRecorder(isFeatureEnabled: featureIsEnabledIfInitialized) |
| 68 | + |
| 69 | + private let recorder: IObservabilityEventRecorder |
| 70 | + |
| 71 | + /// - Parameter isFeatureEnabled: the feature-manager read for a catalog flag key. |
| 72 | + init(isFeatureEnabled: @escaping (String) -> Bool) { |
| 73 | + // Console-only logger: `attach` runs under the remote logger's lifecycle lock, and |
| 74 | + // `OneSignalLog` reaches app listeners synchronously, so a listener that re-enters the |
| 75 | + // SDK would deadlock on that lock. |
| 76 | + recorder = LoggerFactory.shared.createObservabilityEventRecorder( |
| 77 | + flags: OSFeatureFlagReader(isFeatureEnabled: isFeatureEnabled), |
| 78 | + logger: OSConsoleLogger() |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + /// Never constructs the feature manager: first touch latches every APP_STARTUP flag from |
| 83 | + /// whatever storage returns, and a record can arrive from any thread at any time. Off until |
| 84 | + /// the manager exists, which is also the documented state before the first flags fetch. |
| 85 | + static func featureIsEnabledIfInitialized(_ key: String) -> Bool { |
| 86 | + OSFeatureManager.enabledFeatureKeysIfInitialized().contains(key) |
| 87 | + } |
| 88 | + |
| 89 | + public func record(event: OSObservabilityEvent, attributes: [String: String]) { |
| 90 | + recorder.record(event: event.kmpEvent, attributes: attributes) |
| 91 | + } |
| 92 | + |
| 93 | + /// Drops whatever an earlier app id queued; called beside the other app-id-change resets. |
| 94 | + public func reset() { |
| 95 | + recorder.reset() |
| 96 | + } |
| 97 | + |
| 98 | + func attach(_ telemetry: ILogTelemetry) { |
| 99 | + recorder.attach(telemetry: telemetry) |
| 100 | + } |
| 101 | + |
| 102 | + func detach(_ telemetry: ILogTelemetry) { |
| 103 | + recorder.detach(telemetry: telemetry) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/// Answers the KMP recorder's flag lookups; each event's own gate decides which flag to ask about. |
| 108 | +private final class OSFeatureFlagReader: IFeatureFlagReader { |
| 109 | + private let isFeatureEnabled: (String) -> Bool |
| 110 | + |
| 111 | + init(isFeatureEnabled: @escaping (String) -> Bool) { |
| 112 | + self.isFeatureEnabled = isFeatureEnabled |
| 113 | + } |
| 114 | + |
| 115 | + func isEnabled(flag: FeatureFlag) -> Bool { |
| 116 | + isFeatureEnabled(flag.key) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +private extension OSObservabilityEvent { |
| 121 | + var kmpEvent: ObservabilityEvent { |
| 122 | + switch self { |
| 123 | + case .deviceGesture: |
| 124 | + return .deviceGesture |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments