Skip to content

Commit

Permalink
feat: Resolve Debug at DEBUG level logging
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Nov 11, 2024
1 parent df38f0b commit 9e7804b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 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 @@ -397,7 +397,8 @@ extension Confidence {
let options = ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: clientSecret),
region: region,
timeoutIntervalForRequest: timeout)
timeoutIntervalForRequest: timeout,
debugLogger: debugLogger)
let metadata = ConfidenceMetadata(
name: sdkId,
version: "1.0.1") // x-release-please-version
Expand Down
9 changes: 8 additions & 1 deletion Sources/Confidence/ConfidenceClientOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@ 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
timeoutIntervalForRequest: Double,
debugLogger: DebugLogger? = nil
) {
self.credentials = credentials
self.region = region ?? .global
self.initializationStrategy = initializationStrategy
self.timeoutIntervalForRequest = timeoutIntervalForRequest
self.debugLogger = debugLogger
}

func getLogger() -> DebugLogger? {
return debugLogger
}
}

Expand Down
13 changes: 12 additions & 1 deletion Sources/Confidence/RemoteResolveConfidenceClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,25 @@ 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: TypeMapper.convert(structure: ctx),
evaluationContext: ctxNetworkValue,
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

0 comments on commit 9e7804b

Please sign in to comment.