Skip to content

Commit

Permalink
Add TypeMismatch error
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Nov 6, 2024
1 parent 382818a commit d2926be
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 35 deletions.
2 changes: 2 additions & 0 deletions ConfidenceDemoApp/ConfidenceDemoApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ struct ContentView: View {
.padding(10)
Text(text.text)
Button("Get remote flag value") {
let test = confidence.getEvaluation(key: "swift-demoapp.color", defaultValue: true)
print("\(test)")
text.text = confidence.getValue(key: "swift-demoapp.color", defaultValue: "ERROR")
if text.text == "Green" {
color.color = .green
Expand Down
9 changes: 2 additions & 7 deletions Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public class Confidence: ConfidenceEventSender {
internal let remoteFlagResolver: ConfidenceResolveClient
internal let contextReconciliatedChanges = PassthroughSubject<String, Never>()

public static let sdkId: String = "SDK_ID_SWIFT_CONFIDENCE"

required init(
clientSecret: String,
region: ConfidenceRegion,
Expand Down Expand Up @@ -153,8 +151,7 @@ public class Confidence: ConfidenceEventSender {
return self.cache.evaluate(
flagName: key,
defaultValue: defaultValue,
// TMP - TESTING (force a different context, causing STALE)
context: ["test":ConfidenceValue(null: ())],
context: getContext(),
flagApplier: flagApplier
)
}
Expand Down Expand Up @@ -399,9 +396,7 @@ extension Confidence {
credentials: ConfidenceClientCredentials.clientSecret(secret: clientSecret),
region: region,
timeoutIntervalForRequest: timeout)
let metadata = ConfidenceMetadata(
name: sdkId,
version: "1.0.1") // x-release-please-version
let metadata = ConfidenceMetadata.defaultMetadata
let uploader = RemoteConfidenceClient(
options: options,
metadata: metadata,
Expand Down
12 changes: 8 additions & 4 deletions Sources/Confidence/ConfidenceMetadata.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import Foundation

struct ConfidenceMetadata {
private static let sdkName: String = "SDK_ID_SWIFT_CONFIDENCE"
private static let sdkId: Int = 13 // TODO enstablish cross-language identifiers

public var id: Int
public var name: String
public var version: String

public init(name: String, version: String) {
self.name = name
self.version = version
}
public static let defaultMetadata = ConfidenceMetadata(
id: sdkId,
name: sdkName,
version: "1.0.1") // x-release-please-version
}
1 change: 1 addition & 0 deletions Sources/Confidence/FlagEvaluation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ extension FlagResolution {
errorMessage: nil
)
} else {
Telemetry.shared.incrementFlagTypeMismatch()
return Evaluation(
value: defaultValue,
variant: nil,
Expand Down
29 changes: 25 additions & 4 deletions Sources/Confidence/Telemetry/TelemetryManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@ import Foundation

protocol TelemetryManager {
func incrementStaleAccess()
func incrementFlagTypeMismatch()
func getSnapshot() -> TelemetryPayload
}

class Telemetry: TelemetryManager {
private let queue = DispatchQueue(label: "com.confidence.telemetry_manager")
private var staleAccessCounter = 0;

Check failure on line 11 in Sources/Confidence/Telemetry/TelemetryManager.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Semicolon Violation: Lines should not have trailing semicolons (trailing_semicolon)
private var flagTypeMismatchCounter = 0;

Check failure on line 12 in Sources/Confidence/Telemetry/TelemetryManager.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Semicolon Violation: Lines should not have trailing semicolons (trailing_semicolon)

public init() {}

static public let shared: TelemetryManager = Telemetry()
static public let shared: TelemetryManager = Telemetry.init()

public func getSnapshot() -> TelemetryPayload {
return queue.sync {
TelemetryPayload(staleAccess: getStaleAccessAndReset())
}
TelemetryPayload(
libraryId: ConfidenceMetadata.defaultMetadata.id,
libraryVersion: ConfidenceMetadata.defaultMetadata.version,
countTraces: [
CountTrace.init(traceId: TraceId.staleAccess, count: getStaleAccessAndReset()),
CountTrace.init(traceId: TraceId.typeMismatch, count: getFlagTypeMismatchAndReset()),
],
durationsTraces: [])
}

public func incrementStaleAccess() {
Expand All @@ -25,11 +32,25 @@ class Telemetry: TelemetryManager {
}
}

public func incrementFlagTypeMismatch() {
queue.sync {
flagTypeMismatchCounter += 1
}
}

private func getStaleAccessAndReset() -> Int {
return queue.sync {
let currentCounter = staleAccessCounter
staleAccessCounter = 0;

Check failure on line 44 in Sources/Confidence/Telemetry/TelemetryManager.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Semicolon Violation: Lines should not have trailing semicolons (trailing_semicolon)
return currentCounter
}
}

private func getFlagTypeMismatchAndReset() -> Int {
return queue.sync {
let currentCounter = flagTypeMismatchCounter
flagTypeMismatchCounter = 0;

Check failure on line 52 in Sources/Confidence/Telemetry/TelemetryManager.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Semicolon Violation: Lines should not have trailing semicolons (trailing_semicolon)
return currentCounter
}
}
}
20 changes: 19 additions & 1 deletion Sources/Confidence/Telemetry/TelemetryPayload.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import Foundation

struct TelemetryPayload: Encodable {
var staleAccess = 0
var libraryId: Int
var libraryVersion: String
var countTraces: [CountTrace]
var durationsTraces: [DurationsTrace]
}

struct CountTrace: Encodable {
var traceId: TraceId
var count: Int
}

struct DurationsTrace: Encodable {
var traceId: TraceId
var millisDuration: [Int]
}

enum TraceId: Int, Encodable {
case typeMismatch = 1
case staleAccess = 2
}
20 changes: 10 additions & 10 deletions Tests/ConfidenceTests/ConfidenceContextTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class ConfidenceContextTests: XCTestCase {
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: ""), timeoutIntervalForRequest: 10),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))
metadata: ConfidenceMetadata(id: 0, name: "", version: ""))

let confidenceParent = Confidence.init(
clientSecret: "",
Expand All @@ -35,7 +35,7 @@ final class ConfidenceContextTests: XCTestCase {
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: ""), timeoutIntervalForRequest: 10),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))
metadata: ConfidenceMetadata(id: 0, name: "", version: ""))

let confidenceParent = Confidence.init(
clientSecret: "",
Expand Down Expand Up @@ -67,7 +67,7 @@ final class ConfidenceContextTests: XCTestCase {
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: ""), timeoutIntervalForRequest: 10),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))
metadata: ConfidenceMetadata(id: 0, name: "", version: ""))

let confidence = Confidence.init(
clientSecret: "",
Expand All @@ -94,7 +94,7 @@ final class ConfidenceContextTests: XCTestCase {
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: ""), timeoutIntervalForRequest: 10),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))
metadata: ConfidenceMetadata(id: 0, name: "", version: ""))

let confidenceParent = Confidence.init(
clientSecret: "",
Expand Down Expand Up @@ -125,7 +125,7 @@ final class ConfidenceContextTests: XCTestCase {
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: ""), timeoutIntervalForRequest: 10),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))
metadata: ConfidenceMetadata(id: 0, name: "", version: ""))

let confidenceParent = Confidence.init(
clientSecret: "",
Expand Down Expand Up @@ -156,7 +156,7 @@ final class ConfidenceContextTests: XCTestCase {
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: ""), timeoutIntervalForRequest: 10),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))
metadata: ConfidenceMetadata(id: 0, name: "", version: ""))

let confidence = Confidence.init(
clientSecret: "",
Expand All @@ -181,7 +181,7 @@ final class ConfidenceContextTests: XCTestCase {
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: ""), timeoutIntervalForRequest: 10),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))
metadata: ConfidenceMetadata(id: 0, name: "", version: ""))

let confidenceParent = Confidence.init(
clientSecret: "",
Expand Down Expand Up @@ -209,7 +209,7 @@ final class ConfidenceContextTests: XCTestCase {
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: ""), timeoutIntervalForRequest: 10),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))
metadata: ConfidenceMetadata(id: 0, name: "", version: ""))

let confidenceParent = Confidence.init(
clientSecret: "",
Expand Down Expand Up @@ -240,7 +240,7 @@ final class ConfidenceContextTests: XCTestCase {
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: ""), timeoutIntervalForRequest: 10),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))
metadata: ConfidenceMetadata(id: 0, name: "", version: ""))

let confidenceParent = Confidence.init(
clientSecret: "",
Expand Down Expand Up @@ -273,7 +273,7 @@ final class ConfidenceContextTests: XCTestCase {
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: ""), timeoutIntervalForRequest: 10),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))
metadata: ConfidenceMetadata(id: 0, name: "", version: ""))

let confidence = Confidence.init(
clientSecret: "",
Expand Down
4 changes: 2 additions & 2 deletions Tests/ConfidenceTests/EventSenderEngineTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ final class EventSenderEngineTest: XCTestCase {
timeoutIntervalForRequest: 10
),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))
metadata: ConfidenceMetadata(id: 0, name: "", version: ""))

let eventSenderEngine = EventSenderEngineImpl(
clientSecret: "CLIENT_SECRET",
Expand Down Expand Up @@ -143,7 +143,7 @@ final class EventSenderEngineTest: XCTestCase {
timeoutIntervalForRequest: 10
),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))
metadata: ConfidenceMetadata(id: 0, name: "", version: ""))

let eventSenderEngine = EventSenderEngineImpl(
clientSecret: "CLIENT_SECRET",
Expand Down
2 changes: 1 addition & 1 deletion Tests/ConfidenceTests/FlagApplierWithRetriesTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
)
private var storage = StorageMock()
private var httpClient = HttpClientMock()
private let metadata = ConfidenceMetadata(name: "test-provider-name", version: "0.0.0.")
private let metadata = ConfidenceMetadata(id: 0, name: "test-provider-name", version: "0.0.0.")

override func setUp() {
storage = StorageMock()
Expand Down
8 changes: 4 additions & 4 deletions Tests/ConfidenceTests/RemoteConfidenceClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RemoteConfidenceClientTest: XCTestCase {
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: ""), timeoutIntervalForRequest: 10),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))
metadata: ConfidenceMetadata(id: 0, name: "", version: ""))

let processed = try await client.upload(events: [
NetworkEvent(
Expand All @@ -31,7 +31,7 @@ class RemoteConfidenceClientTest: XCTestCase {
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: ""), timeoutIntervalForRequest: 10),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))
metadata: ConfidenceMetadata(id: 0, name: "", version: ""))

let processed = try await client.upload(events: [])
XCTAssertTrue(processed)
Expand All @@ -43,7 +43,7 @@ class RemoteConfidenceClientTest: XCTestCase {
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: ""), timeoutIntervalForRequest: 10),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))
metadata: ConfidenceMetadata(id: 0, name: "", version: ""))

let processed = try await client.upload(events: [
NetworkEvent(
Expand All @@ -61,7 +61,7 @@ class RemoteConfidenceClientTest: XCTestCase {
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: ""), timeoutIntervalForRequest: 10),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))
metadata: ConfidenceMetadata(id: 0, name: "", version: ""))

var caughtError: ConfidenceError?
do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RemoteResolveConfidenceClientTest: XCTestCase {
options: .init(credentials: .clientSecret(secret: "test"), timeoutIntervalForRequest: 10),
session: session,
applyOnResolve: true,
metadata: ConfidenceMetadata(name: "", version: "")
metadata: ConfidenceMetadata(id: 0, name: "", version: "")
)

let context = ["targeting_key": ConfidenceValue(string: "user1")]
Expand Down
2 changes: 1 addition & 1 deletion release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"README.md",
"Sources/ConfidenceProvider/README.md",
"Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift",
"Sources/Confidence/Confidence.swift"
"Sources/Confidence/ConfidenceMetadata.swift"
]
}
},
Expand Down

0 comments on commit d2926be

Please sign in to comment.