Skip to content

Commit

Permalink
Log errors to console when reserved keys are being used by users
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Nov 1, 2024
1 parent 8deeddc commit 1490fea
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Sources/TelemetryDeck/Signals/SignalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ protocol SignalManageable {
final class SignalManager: SignalManageable, @unchecked Sendable {
static let minimumSecondsToPassBetweenRequests: Double = 10

static let reservedKeysLowercased: Set<String> = Set(
[
"type", "clientUser", "appID", "sessionID", "floatValue",
"newSessionBegan", "platform", "systemVersion", "majorSystemVersion", "majorMinorSystemVersion", "appVersion", "buildNumber",
"isSimulator", "isDebug", "isTestFlight", "isAppStore", "modelName", "architecture", "operatingSystem", "targetEnvironment",
"locale", "region", "appLanguage", "preferredLanguage", "telemetryClientVersion",
].map { $0.lowercased() }
)

private var signalCache: SignalCache<SignalPostBody>
let configuration: TelemetryManagerConfiguration

Expand Down Expand Up @@ -75,6 +84,24 @@ final class SignalManager: SignalManageable, @unchecked Sendable {
customUserID: String?,
configuration: TelemetryManagerConfiguration
) {
// warn users about reserved keys to avoid unexpected behavior
if signalName.lowercased().hasPrefix("telemetrydeck.") || Self.reservedKeysLowercased.contains(signalName.lowercased()) {
configuration.logHandler?.log(
.error,
message: "Sending signal with reserved key '\(signalName)' will cause unexpected behavior. Please use another name instead."
)
}

for parameterKey in parameters.keys {
if parameterKey.lowercased().hasPrefix("telemetrydeck.") || Self.reservedKeysLowercased.contains(parameterKey.lowercased()) {
configuration.logHandler?.log(
.error,
message: "Sending parameter with reserved key '\(parameterKey)' will cause unexpected behavior. Please use another key instead."
)
}
}

// enqueue signal to sending cache
DispatchQueue.main.async {
let defaultUserIdentifier = self.defaultUserIdentifier
let defaultParameters = DefaultSignalPayload.parameters
Expand Down

0 comments on commit 1490fea

Please sign in to comment.