Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log errors to console when reserved keys are being used by users #197

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 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,34 @@ final class SignalManager: SignalManageable, @unchecked Sendable {
customUserID: String?,
configuration: TelemetryManagerConfiguration
) {
// warn users about reserved keys to avoid unexpected behavior
if signalName.lowercased().hasPrefix("telemetrydeck.") {
configuration.logHandler?.log(
.error,
message: "Sending signal with reserved prefix 'TelemetryDeck.' will cause unexpected behavior. Please use another prefix instead."
)
} else if Self.reservedKeysLowercased.contains(signalName.lowercased()) {
configuration.logHandler?.log(
.error,
message: "Sending signal with reserved name '\(signalName)' will cause unexpected behavior. Please use another name instead."
)
}

for parameterKey in parameters.keys {
if parameterKey.lowercased().hasPrefix("telemetrydeck.") {
configuration.logHandler?.log(
.error,
message: "Sending parameter with reserved key prefix 'TelemetryDeck.' will cause unexpected behavior. Please use another prefix instead."
)
} else if 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