Skip to content

Commit

Permalink
Merge branch 'main' into put-context-spec
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria authored Nov 12, 2024
2 parents 90f06f1 + 558c811 commit 4553324
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
5 changes: 4 additions & 1 deletion ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ struct ConfidenceDemoApp: App {
WindowGroup {
let secret = ProcessInfo.processInfo.environment["CLIENT_SECRET"] ?? ""
let confidence = Confidence.Builder(clientSecret: secret, loggerLevel: .TRACE)
.withContext(initialContext: ["targeting_key": ConfidenceValue(string: UUID.init().uuidString)])
.withContext(initialContext: [
"targeting_key": ConfidenceValue(string: UUID.init().uuidString),
"user_id": .init(string: "user2")
])
.build()

let status = Status()
Expand Down
3 changes: 2 additions & 1 deletion Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ public class Confidence: ConfidenceEventSender {
flagName: key,
defaultValue: defaultValue,
context: getContext(),
flagApplier: flagApplier
flagApplier: flagApplier,
debugLogger: debugLogger
)
}
}
Expand Down
11 changes: 11 additions & 0 deletions Sources/Confidence/DebugLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ internal protocol DebugLogger {
func logMessage(message: String, isWarning: Bool)
func logFlags(action: String, flag: String)
func logContext(action: String, context: ConfidenceStruct)
func logResolveDebugURL(flagName: String, context: ConfidenceStruct)
}

private extension Logger {
Expand All @@ -15,6 +16,16 @@ private extension Logger {
}

internal class DebugLoggerImpl: DebugLogger {
private let encoder = JSONEncoder()

func logResolveDebugURL(flagName: String, context: ConfidenceStruct) {
let ctxNetworkValue = TypeMapper.convert(structure: context)
if let ctxNetworkData = try? encoder.encode(ctxNetworkValue),
let ctxNetworkString = String(data: ctxNetworkData, encoding: .utf8) {
log(messageLevel: .DEBUG, message: "[Resolve Debug] https://app.confidence.spotify.com/flags/resolver-test?flag=flags/\(flagName)&context=\(ctxNetworkString)")
}
}

private let loggerLevel: LoggerLevel

init(loggerLevel: LoggerLevel) {
Expand Down
7 changes: 6 additions & 1 deletion Sources/Confidence/FlagEvaluation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ extension FlagResolution {
flagName: String,
defaultValue: T,
context: ConfidenceStruct,
flagApplier: FlagApplier? = nil
flagApplier: FlagApplier? = nil,
debugLogger: DebugLogger? = nil
) -> Evaluation<T> {
do {
let parsedKey = try FlagPath.getPath(for: flagName)
Expand All @@ -44,6 +45,10 @@ extension FlagResolution {
)
}

if let debugLogger = debugLogger {
debugLogger.logResolveDebugURL(flagName: parsedKey.flag, context: context)
}

if let evaluation = checkBackendErrors(resolvedFlag: resolvedFlag, defaultValue: defaultValue) {
return evaluation
}
Expand Down
4 changes: 4 additions & 0 deletions Tests/ConfidenceTests/Helpers/DebugLoggerFake.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ internal class DebugLoggerFake: DebugLogger {
// no-op
}

func logResolveDebugURL(flagName: String, context: ConfidenceStruct) {
// no-op
}

func getUploadBatchSuccessCount() -> Int {
return uploadBatchSuccessCounter.get()
}
Expand Down

0 comments on commit 4553324

Please sign in to comment.