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

feat: Resolve Debug at DEBUG level logging #174

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
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
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
Loading