From 558c8112210b16572f76f2636c1fdaafe1a4bdb6 Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Tue, 12 Nov 2024 10:41:56 +0100 Subject: [PATCH] feat: Resolve Debug at DEBUG level logging (#174) * feat: Resolve Debug at DEBUG level logging * feat: Move debug resolve to get evaluation --- .../ConfidenceDemoApp/ConfidenceDemoApp.swift | 5 ++++- Sources/Confidence/Confidence.swift | 3 ++- Sources/Confidence/DebugLogger.swift | 11 +++++++++++ Sources/Confidence/FlagEvaluation.swift | 7 ++++++- Tests/ConfidenceTests/Helpers/DebugLoggerFake.swift | 4 ++++ 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift b/ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift index 073efd95..6d79beb7 100644 --- a/ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift +++ b/ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift @@ -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() diff --git a/Sources/Confidence/Confidence.swift b/Sources/Confidence/Confidence.swift index f5e5dc75..8e5d8896 100644 --- a/Sources/Confidence/Confidence.swift +++ b/Sources/Confidence/Confidence.swift @@ -154,7 +154,8 @@ public class Confidence: ConfidenceEventSender { flagName: key, defaultValue: defaultValue, context: getContext(), - flagApplier: flagApplier + flagApplier: flagApplier, + debugLogger: debugLogger ) } } diff --git a/Sources/Confidence/DebugLogger.swift b/Sources/Confidence/DebugLogger.swift index fe7922c2..b5dbd92f 100644 --- a/Sources/Confidence/DebugLogger.swift +++ b/Sources/Confidence/DebugLogger.swift @@ -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 { @@ -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) { diff --git a/Sources/Confidence/FlagEvaluation.swift b/Sources/Confidence/FlagEvaluation.swift index d64e43ca..611aa26a 100644 --- a/Sources/Confidence/FlagEvaluation.swift +++ b/Sources/Confidence/FlagEvaluation.swift @@ -29,7 +29,8 @@ extension FlagResolution { flagName: String, defaultValue: T, context: ConfidenceStruct, - flagApplier: FlagApplier? = nil + flagApplier: FlagApplier? = nil, + debugLogger: DebugLogger? = nil ) -> Evaluation { do { let parsedKey = try FlagPath.getPath(for: flagName) @@ -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 } diff --git a/Tests/ConfidenceTests/Helpers/DebugLoggerFake.swift b/Tests/ConfidenceTests/Helpers/DebugLoggerFake.swift index db9ddb55..20d24a95 100644 --- a/Tests/ConfidenceTests/Helpers/DebugLoggerFake.swift +++ b/Tests/ConfidenceTests/Helpers/DebugLoggerFake.swift @@ -25,6 +25,10 @@ internal class DebugLoggerFake: DebugLogger { // no-op } + func logResolveDebugURL(flagName: String, context: ConfidenceStruct) { + // no-op + } + func getUploadBatchSuccessCount() -> Int { return uploadBatchSuccessCounter.get() }