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

Update to Ingest API v2 #179

Merged
merged 2 commits into from
Aug 13, 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
2 changes: 1 addition & 1 deletion Sources/TelemetryClient/Signal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal struct SignalPostBody: Codable, Equatable {
let floatValue: Double?

/// Tags in the form "key:value" to attach to the signal
let payload: [String]
let payload: [String: String]

/// If "true", mark the signal as a testing signal and only show it in a dedicated test mode UI
let isTestMode: String
Expand Down
6 changes: 0 additions & 6 deletions Sources/TelemetryClient/SignalEnricher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,4 @@ extension Dictionary where Key == String, Value == String {
other
}
}

func toMultiValueDimension() -> [String] {
map { key, value in
key.replacingOccurrences(of: ":", with: "_") + ":" + value
}
}
}
4 changes: 2 additions & 2 deletions Sources/TelemetryClient/SignalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ internal final class SignalManager: SignalManageable, @unchecked Sendable {
sessionID: configuration.sessionID.uuidString,
type: "\(signalName)",
floatValue: floatValue,
payload: payload.toMultiValueDimension(),
payload: payload,
isTestMode: configuration.testMode ? "true" : "false"
)

Expand Down Expand Up @@ -184,7 +184,7 @@ private extension SignalManager {
private extension SignalManager {
private func send(_ signalPostBodies: [SignalPostBody], completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) {
DispatchQueue.global(qos: .utility).async {
let path = "/api/v1/apps/\(self.configuration.telemetryAppID)/signals/multiple/"
let path = "/v2/"
let url = self.configuration.apiBaseURL.appendingPathComponent(path)

var urlRequest = URLRequest(url: url)
Expand Down
47 changes: 23 additions & 24 deletions Tests/TelemetryClientTests/TelemetryClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import XCTest

final class TelemetryClientTests: XCTestCase {

func testSending() {
let YOUR_APP_ID = "44e0f59a-60a2-4d4a-bf27-1f96ccb4aaa3"

Expand All @@ -17,21 +16,21 @@ final class TelemetryClientTests: XCTestCase {
let signalCache = SignalCache<SignalPostBody>(logHandler: nil)

let signals: [SignalPostBody] = [
.init(receivedAt: Date(), appID: UUID(), clientUser: "01", sessionID: "01", type: "test", floatValue: nil, payload: [], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "02", sessionID: "02", type: "test", floatValue: nil, payload: [], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "03", sessionID: "03", type: "test", floatValue: nil, payload: [], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "04", sessionID: "04", type: "test", floatValue: nil, payload: [], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "05", sessionID: "05", type: "test", floatValue: nil, payload: [], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "06", sessionID: "06", type: "test", floatValue: nil, payload: [], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "07", sessionID: "07", type: "test", floatValue: nil, payload: [], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "08", sessionID: "08", type: "test", floatValue: nil, payload: [], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "09", sessionID: "09", type: "test", floatValue: nil, payload: [], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "10", sessionID: "10", type: "test", floatValue: nil, payload: [], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "11", sessionID: "11", type: "test", floatValue: nil, payload: [], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "12", sessionID: "12", type: "test", floatValue: nil, payload: [], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "13", sessionID: "13", type: "test", floatValue: nil, payload: [], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "14", sessionID: "14", type: "test", floatValue: nil, payload: [], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "15", sessionID: "15", type: "test", floatValue: nil, payload: [], isTestMode: "true")
.init(receivedAt: Date(), appID: UUID(), clientUser: "01", sessionID: "01", type: "test", floatValue: nil, payload: [:], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "02", sessionID: "02", type: "test", floatValue: nil, payload: [:], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "03", sessionID: "03", type: "test", floatValue: nil, payload: [:], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "04", sessionID: "04", type: "test", floatValue: nil, payload: [:], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "05", sessionID: "05", type: "test", floatValue: nil, payload: [:], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "06", sessionID: "06", type: "test", floatValue: nil, payload: [:], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "07", sessionID: "07", type: "test", floatValue: nil, payload: [:], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "08", sessionID: "08", type: "test", floatValue: nil, payload: [:], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "09", sessionID: "09", type: "test", floatValue: nil, payload: [:], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "10", sessionID: "10", type: "test", floatValue: nil, payload: [:], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "11", sessionID: "11", type: "test", floatValue: nil, payload: [:], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "12", sessionID: "12", type: "test", floatValue: nil, payload: [:], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "13", sessionID: "13", type: "test", floatValue: nil, payload: [:], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "14", sessionID: "14", type: "test", floatValue: nil, payload: [:], isTestMode: "true"),
.init(receivedAt: Date(), appID: UUID(), clientUser: "15", sessionID: "15", type: "test", floatValue: nil, payload: [:], isTestMode: "true")
]

for signal in signals {
Expand All @@ -56,7 +55,7 @@ final class TelemetryClientTests: XCTestCase {

func testSignalEnrichers() throws {
struct BasicEnricher: SignalEnricher {
func enrich(signalType: String, for clientUser: String?, floatValue: Double?) -> [String : String] {
func enrich(signalType: String, for clientUser: String?, floatValue: Double?) -> [String: String] {
["isTestEnricher": "true"]
}
}
Expand All @@ -71,12 +70,12 @@ final class TelemetryClientTests: XCTestCase {
let bodyItems = signalManager.processedSignals
XCTAssertEqual(bodyItems.count, 1)
let bodyItem = try XCTUnwrap(bodyItems.first)
XCTAssert(bodyItem.payload.contains("isTestEnricher:true"))
XCTAssert(bodyItem.payload["isTestEnricher"] == "true")
}

func testSignalEnrichers_precedence() throws {
struct BasicEnricher: SignalEnricher {
func enrich(signalType: String, for clientUser: String?, floatValue: Double?) -> [String : String] {
func enrich(signalType: String, for clientUser: String?, floatValue: Double?) -> [String: String] {
["item": "A", "isDebug": "banana"]
}
}
Expand All @@ -91,8 +90,8 @@ final class TelemetryClientTests: XCTestCase {
let bodyItems = signalManager.processedSignals
XCTAssertEqual(bodyItems.count, 1)
let bodyItem = try XCTUnwrap(bodyItems.first)
XCTAssert(bodyItem.payload.contains("item:B")) // .send takes priority over enricher
XCTAssert(bodyItem.payload.contains("isDebug:banana")) // enricher takes priority over default payload
XCTAssert(bodyItem.payload["item"] == "B") // .send takes priority over enricher
XCTAssert(bodyItem.payload["isDebug"] == "banana") // enricher takes priority over default payload
}

func testSendsSignals_withAnalyticsImplicitlyEnabled() {
Expand Down Expand Up @@ -173,11 +172,11 @@ private class FakeSignalManager: SignalManageable {
var processedSignals = [SignalPostBody]()

@MainActor
func processSignal(_ signalType: String, parameters: [String : String], floatValue: Double?, customUserID: String?, configuration: TelemetryManagerConfiguration) {
func processSignal(_ signalType: String, parameters: [String: String], floatValue: Double?, customUserID: String?, configuration: TelemetryManagerConfiguration) {
processedSignalTypes.append(signalType)
let enrichedMetadata: [String: String] = configuration.metadataEnrichers
.map { $0.enrich(signalType: signalType, for: customUserID, floatValue: floatValue) }
.reduce([String: String](), { $0.applying($1) })
.reduce([String: String]()) { $0.applying($1) }

let payload = DefaultSignalPayload.parameters
.applying(enrichedMetadata)
Expand All @@ -190,7 +189,7 @@ private class FakeSignalManager: SignalManageable {
sessionID: configuration.sessionID.uuidString,
type: "\(signalType)",
floatValue: floatValue,
payload: payload.toMultiValueDimension(),
payload: payload,
isTestMode: configuration.testMode ? "true" : "false"
)
processedSignals.append(signalPostBody)
Expand Down