Skip to content

Commit

Permalink
Merge pull request #221 from TelemetryDeck/feature/suppress-warnings
Browse files Browse the repository at this point in the history
Add new option to allow other SDKs to suppress reserved key warnings
  • Loading branch information
Jeehut authored Jan 14, 2025
2 parents 04d2de6 + 072aea6 commit b8486e4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
5 changes: 5 additions & 0 deletions Sources/TelemetryDeck/TelemetryClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public final class TelemetryManagerConfiguration: @unchecked Sendable {
/// However it won't interfere with SwiftUI Previews, when explicitly settings this value to `false`.
public var analyticsDisabled: Bool = false

/// Determines whether to log warnings when user-provided custom parameters use reserved parameter names that are internal to TelemetryDeck SDKs.
///
/// - NOTE: Do not change this property if you're using our SDK in your app. This is for usage from other TelemetryDeck SDKs only.
public var reservedParameterWarningsEnabled: Bool = true

/// Log the current status to the signal cache to the console.
@available(*, deprecated, message: "Please use the logHandler property instead")
public var showDebugLogs: Bool = false
Expand Down
42 changes: 22 additions & 20 deletions Sources/TelemetryDeck/TelemetryDeck.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,38 +48,40 @@ public enum TelemetryDeck {
let combinedSignalName = (configuration.defaultSignalPrefix ?? "") + signalName
let prefixedParameters = parameters.mapKeys { (configuration.defaultParameterPrefix ?? "") + $0 }

// warn users about reserved keys to avoid unexpected behavior
if combinedSignalName.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(combinedSignalName.lowercased()) {
configuration.logHandler?.log(
.error,
message: "Sending signal with reserved name '\(combinedSignalName)' will cause unexpected behavior. Please use another name instead."
)
}

// only check parameters (not default ones)
for parameterKey in prefixedParameters.keys {
if parameterKey.lowercased().hasPrefix("telemetrydeck.") {
if configuration.reservedParameterWarningsEnabled {
// warn users about reserved keys to avoid unexpected behavior
if combinedSignalName.lowercased().hasPrefix("telemetrydeck.") {
configuration.logHandler?.log(
.error,
message: "Sending parameter with reserved key prefix 'TelemetryDeck.' will cause unexpected behavior. Please use another prefix instead."
message: "Sending signal with reserved prefix 'TelemetryDeck.' will cause unexpected behavior. Please use another prefix instead."
)
} else if Self.reservedKeysLowercased.contains(parameterKey.lowercased()) {
} else if Self.reservedKeysLowercased.contains(combinedSignalName.lowercased()) {
configuration.logHandler?.log(
.error,
message: "Sending parameter with reserved key '\(parameterKey)' will cause unexpected behavior. Please use another key instead."
message: "Sending signal with reserved name '\(combinedSignalName)' will cause unexpected behavior. Please use another name instead."
)
}

// only check parameters (not default ones)
for parameterKey in prefixedParameters.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."
)
}
}
}

self.internalSignal(combinedSignalName, parameters: prefixedParameters, floatValue: floatValue, customUserID: customUserID)
}

/// A signal being sent without enriching the signal name with a prefix. Also, any reserved signal name check are skipped. Only for internal use.
/// A signal being sent without enriching the signal name with a prefix. Also, any reserved signal name checks are skipped. Only for internal use.
static func internalSignal(
_ signalName: String,
parameters: [String: String] = [:],
Expand Down

0 comments on commit b8486e4

Please sign in to comment.