Skip to content

Commit

Permalink
Simplifying flush on error logic and improving destination plugin nam…
Browse files Browse the repository at this point in the history
…es (#374)
  • Loading branch information
MichaelGHSeg authored Nov 22, 2024
1 parent f6acb75 commit cc47b9a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
1 change: 1 addition & 0 deletions Sources/Segment/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ extension Analytics {
Telemetry.shared.error(metric: Telemetry.INVOKE_ERROR_METRIC, log: Thread.callStackSymbols.joined(separator: "\n")) {
(_ it: inout [String: String]) in
it["error"] = "\(translatedError)"
it["caller"] = Thread.callStackSymbols[3]
}
}
}
20 changes: 15 additions & 5 deletions Sources/Segment/Timeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ internal class Mediator {
Telemetry.shared.increment(metric: Telemetry.INTEGRATION_METRIC) {
(_ it: inout [String: String]) in
it["message"] = "added"
it["plugin"] = "\(plugin.type)-\(String(describing: plugin))"
if let plugin = plugin as? DestinationPlugin, !plugin.key.isEmpty {
it["plugin"] = "\(plugin.type)-\(plugin.key)"
} else {
it["plugin"] = "\(plugin.type)-\(String(describing: plugin))"
}
}
}

Expand All @@ -77,8 +81,11 @@ internal class Mediator {
Telemetry.shared.increment(metric: Telemetry.INTEGRATION_METRIC) {
(_ it: inout [String: String]) in
it["message"] = "removed"
it["plugin"] = "\(plugin.type)-\(String(describing: plugin))"
}
if let plugin = plugin as? DestinationPlugin, !plugin.key.isEmpty {
it["plugin"] = "\(plugin.type)-\(plugin.key)"
} else {
it["plugin"] = "\(plugin.type)-\(String(describing: plugin))"
} }
return plugin === storedPlugin
}
}
Expand All @@ -99,8 +106,11 @@ internal class Mediator {
Telemetry.shared.increment(metric: Telemetry.INTEGRATION_METRIC) {
(_ it: inout [String: String]) in
it["message"] = "event-\(r.type ?? "unknown")"
it["plugin"] = "\(plugin.type)-\(String(describing: plugin))"
}
if let plugin = plugin as? DestinationPlugin, !plugin.key.isEmpty {
it["plugin"] = "\(plugin.type)-\(plugin.key)"
} else {
it["plugin"] = "\(plugin.type)-\(String(describing: plugin))"
} }
}
}

Expand Down
32 changes: 10 additions & 22 deletions Sources/Segment/Utilities/Telemetry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public class Telemetry: Subscriber {
private var seenErrors = [String: Int]()
internal var started = false
private var rateLimitEndTime: TimeInterval = 0
internal var flushFirstError = true
private var telemetryQueue = DispatchQueue(label: "telemetryQueue")
private var updateQueue = DispatchQueue(label: "updateQueue")
private var telemetryTimer: QueueTimer?
Expand All @@ -98,15 +99,10 @@ public class Telemetry: Subscriber {
guard enable, !started, sampleRate > 0.0 && sampleRate <= 1.0 else { return }
started = true

// Queue contents were sampled at the default 100%
// the values on flush will be adjusted in the send function
if Double.random(in: 0...1) > sampleRate {
resetQueue()
} else {
telemetryQueue.async {
self.queue = self.queue.map { var metric = $0
metric.value = Int(Double(metric.value) / self.sampleRate)
return metric
}
}
}

self.telemetryTimer = QueueTimer(interval: .seconds(self.flushTimer), queue: .main) { [weak self] in
Expand All @@ -133,13 +129,12 @@ public class Telemetry: Subscriber {
/// - buildTags: A closure to build the tags dictionary.
func increment(metric: String, buildTags: (inout [String: String]) -> Void) {
guard enable, sampleRate > 0.0 && sampleRate <= 1.0, metric.hasPrefix(Telemetry.METRICS_BASE_TAG), queueHasSpace() else { return }
if Double.random(in: 0...1) > sampleRate { return }

var tags = [String: String]()
buildTags(&tags)
guard !tags.isEmpty else { return }

if Double.random(in: 0...1) > sampleRate { return }

addRemoteMetric(metric: metric, tags: tags)
}

Expand All @@ -150,6 +145,7 @@ public class Telemetry: Subscriber {
/// - buildTags: A closure to build the tags dictionary.
func error(metric: String, log: String, buildTags: (inout [String: String]) -> Void) {
guard enable, sampleRate > 0.0 && sampleRate <= 1.0, metric.hasPrefix(Telemetry.METRICS_BASE_TAG), queueHasSpace() else { return }
if Double.random(in: 0...1) > sampleRate { return }

var tags = [String: String]()
buildTags(&tags)
Expand All @@ -165,19 +161,11 @@ public class Telemetry: Subscriber {
logData = String(log.prefix(errorLogSizeMax))
}

if let errorKey = tags["error"] {
if let count = seenErrors[errorKey] {
seenErrors[errorKey] = count + 1
if Double.random(in: 0...1) > sampleRate { return }
addRemoteMetric(metric: metric, tags: filteredTags, value: Int(Double(count) * sampleRate), log: logData)
seenErrors[errorKey] = 0
} else {
addRemoteMetric(metric: metric, tags: filteredTags, log: logData)
flush()
seenErrors[errorKey] = 0
}
} else {
addRemoteMetric(metric: metric, tags: filteredTags, log: logData)
addRemoteMetric(metric: metric, tags: filteredTags, log: logData)

if (flushFirstError) {
flushFirstError = false
flush()
}
}

Expand Down
1 change: 1 addition & 0 deletions Tests/Segment-Tests/Telemetry_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class TelemetryTests: XCTestCase {

func testHTTPException() {
mockTelemetryHTTPClient(shouldThrow: true)
Telemetry.shared.flushFirstError = true
Telemetry.shared.enable = true
Telemetry.shared.start()
Telemetry.shared.error(metric: Telemetry.INVOKE_METRIC, log: "log") { $0["error"] = "test" }
Expand Down

0 comments on commit cc47b9a

Please sign in to comment.