Skip to content

Commit

Permalink
Fixed possible duplicates between metrics and attributes (#28)
Browse files Browse the repository at this point in the history
* Fixed possible duplicates between metrics and attributes

* Minor change
  • Loading branch information
AllDmeat authored Dec 27, 2024
1 parent 12462bb commit 7138baf
Showing 1 changed file with 59 additions and 46 deletions.
105 changes: 59 additions & 46 deletions Sources/BlackBoxFirebasePerformance/FirebasePerformanceLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public class FirebasePerformanceLogger: BBLoggerProtocol {
guard let trace = Performance.startTrace(name: traceName) else { return }

event.userInfo.map {
self.addAttributes(to: trace, from: $0)
self.setMetrics(to: trace, from: $0)
self.setMetricsOrAddAttributes(to: trace, from: $0)
}
incrementMetricForParentEvent(of: event)

Expand All @@ -47,8 +46,7 @@ public class FirebasePerformanceLogger: BBLoggerProtocol {
guard let trace = traces.read ({ $0[id] }) else { return }

event.userInfo.map {
self.addAttributes(to: trace, from: $0)
self.setMetrics(to: trace, from: $0)
self.setMetricsOrAddAttributes(to: trace, from: $0)
}

trace.stop()
Expand All @@ -57,62 +55,77 @@ public class FirebasePerformanceLogger: BBLoggerProtocol {
}
}

// MARK: Attributes
extension FirebasePerformanceLogger {
// https://firebase.google.com/docs/perf-mon/custom-code-traces?platform=ios
private func addAttributes(to trace: Trace, from userInfo: BBUserInfo) {
userInfo.forEach { attributeName, value in
guard let stringifiedValue = stringifiedValue(from: value) else { return }

trace.setValue(stringifiedValue, forAttribute: attributeName)
func setMetricsOrAddAttributes(to trace: Trace, from userInfo: BBUserInfo) {
userInfo.forEach { key, value in
if let metric = Metric((key, value)) {
trace.setValue(metric.value, forMetric: metric.name)
} else if let attribute = Attribute((key, value)) {
// https://firebase.google.com/docs/perf-mon/custom-code-traces?platform=ios
trace.setValue(attribute.value, forAttribute: attribute.name)
}
}
}

private func stringifiedValue(from value: Any) -> String? {
switch value {
case Optional<Any>.none:
return nil
case let value as String:
return value
case let value as Error:
return String(reflecting: value)
case _ as Int:
return nil
default:
return String(describing: value)
}

// MARK: - Attributes
extension FirebasePerformanceLogger {
struct Attribute {
let name: String
let value: String

init(name: String, value: String) {
self.name = name
self.value = value
}

init?(_ pair: (key: String, value: Any)) {
let stringValue: String
switch pair.value {
case Optional<Any>.none:
return nil
case let value as String:
stringValue = value
case let value as Error:
stringValue = String(reflecting: value)
default:
stringValue = String(describing: pair.value)
}

self.init(name: pair.key, value: stringValue)
}
}
}

// MARK: Metrics
// MARK: - Metrics
extension FirebasePerformanceLogger {
struct Metric {
let name: String
let value: Int64
}

private func setMetrics(to trace: Trace, from userInfo: BBUserInfo) {
let mterics = metrics(from: userInfo)
mterics.forEach { trace.setValue($0.value, forMetric: $0.name) }
}

private func metrics(from userInfo: BBUserInfo) -> [Metric] {
userInfo.compactMap { key, value in
guard let value = int64Value(from: value) else { return nil }
return Metric(name: key, value: value)

init(name: String, value: Int64) {
self.name = name
self.value = value
}
}

private func int64Value(from value: Any) -> Int64? {
if let value = value as? Int {
return Int64(value)
} else if let value = value as? UInt {
return Int64(value)
} else {
return nil

init?(_ pair: (key: String, value: Any)) {
let intValue: Int64
switch pair.value {
case let value as Int:
intValue = Int64(value)
case let value as UInt:
intValue = Int64(value)
default:
return nil
}

self.init(name: pair.key, value: intValue)
}
}

}

// MARK: Parent Event Metrics
extension FirebasePerformanceLogger {
private func incrementMetricForParentEvent(of event: BlackBox.GenericEvent) {
guard let parentEvent = event.parentEvent else { return }
guard let trace = traces.read({ $0[parentEvent.id] }) else { return }
Expand Down

0 comments on commit 7138baf

Please sign in to comment.