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

Simplifying flush on error logic and improving destination plugin names #374

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
private 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
Loading