Skip to content

Commit

Permalink
feat: Move debug resolve to get evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Nov 12, 2024
1 parent 9e7804b commit 5c617fc
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 24 deletions.
6 changes: 3 additions & 3 deletions 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 Expand Up @@ -397,8 +398,7 @@ extension Confidence {
let options = ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: clientSecret),
region: region,
timeoutIntervalForRequest: timeout,
debugLogger: debugLogger)
timeoutIntervalForRequest: timeout)
let metadata = ConfidenceMetadata(
name: sdkId,
version: "1.0.1") // x-release-please-version
Expand Down
9 changes: 1 addition & 8 deletions Sources/Confidence/ConfidenceClientOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,17 @@ struct ConfidenceClientOptions {
public var region: ConfidenceRegion
public var initializationStrategy: InitializationStrategy
public var timeoutIntervalForRequest: Double
private let debugLogger: DebugLogger?

public init(
credentials: ConfidenceClientCredentials,
region: ConfidenceRegion? = nil,
initializationStrategy: InitializationStrategy = .fetchAndActivate,
timeoutIntervalForRequest: Double,
debugLogger: DebugLogger? = nil
timeoutIntervalForRequest: Double
) {
self.credentials = credentials
self.region = region ?? .global
self.initializationStrategy = initializationStrategy
self.timeoutIntervalForRequest = timeoutIntervalForRequest
self.debugLogger = debugLogger
}

func getLogger() -> DebugLogger? {
return 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
13 changes: 1 addition & 12 deletions Sources/Confidence/RemoteResolveConfidenceClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,14 @@ class RemoteConfidenceResolveClient: ConfidenceResolveClient {
// MARK: Resolver

public func resolve(flags: [String], ctx: ConfidenceStruct) async throws -> ResolvesResult {
let ctxNetworkValue = TypeMapper.convert(structure: ctx)
let request = ResolveFlagsRequest(
flags: flags.map { "flags/\($0)" },
evaluationContext: ctxNetworkValue,
evaluationContext: TypeMapper.convert(structure: ctx),
clientSecret: options.credentials.getSecret(),
apply: applyOnResolve,
sdk: Sdk(id: metadata.name, version: metadata.version)
)

if let debugLogger = options.getLogger() {
let encoder = JSONEncoder()
if let jsonData = try? encoder.encode(ctxNetworkValue),
let jsonCtx = String(data: jsonData, encoding: .utf8) {
debugLogger.logMessage(
message: "[Resolve Debug] https://app.confidence.spotify.com/flags/resolver-test?context=\(jsonCtx)",
isWarning: false)
}
}

do {
let result: HttpClientResult<ResolveFlagsResponse> =
try await self.httpClient.post(path: ":resolve", data: request)
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 5c617fc

Please sign in to comment.