Skip to content

Commit cc47b9a

Browse files
authored
Simplifying flush on error logic and improving destination plugin names (#374)
1 parent f6acb75 commit cc47b9a

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

Sources/Segment/Errors.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ extension Analytics {
9292
Telemetry.shared.error(metric: Telemetry.INVOKE_ERROR_METRIC, log: Thread.callStackSymbols.joined(separator: "\n")) {
9393
(_ it: inout [String: String]) in
9494
it["error"] = "\(translatedError)"
95+
it["caller"] = Thread.callStackSymbols[3]
9596
}
9697
}
9798
}

Sources/Segment/Timeline.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ internal class Mediator {
6868
Telemetry.shared.increment(metric: Telemetry.INTEGRATION_METRIC) {
6969
(_ it: inout [String: String]) in
7070
it["message"] = "added"
71-
it["plugin"] = "\(plugin.type)-\(String(describing: plugin))"
71+
if let plugin = plugin as? DestinationPlugin, !plugin.key.isEmpty {
72+
it["plugin"] = "\(plugin.type)-\(plugin.key)"
73+
} else {
74+
it["plugin"] = "\(plugin.type)-\(String(describing: plugin))"
75+
}
7276
}
7377
}
7478

@@ -77,8 +81,11 @@ internal class Mediator {
7781
Telemetry.shared.increment(metric: Telemetry.INTEGRATION_METRIC) {
7882
(_ it: inout [String: String]) in
7983
it["message"] = "removed"
80-
it["plugin"] = "\(plugin.type)-\(String(describing: plugin))"
81-
}
84+
if let plugin = plugin as? DestinationPlugin, !plugin.key.isEmpty {
85+
it["plugin"] = "\(plugin.type)-\(plugin.key)"
86+
} else {
87+
it["plugin"] = "\(plugin.type)-\(String(describing: plugin))"
88+
} }
8289
return plugin === storedPlugin
8390
}
8491
}
@@ -99,8 +106,11 @@ internal class Mediator {
99106
Telemetry.shared.increment(metric: Telemetry.INTEGRATION_METRIC) {
100107
(_ it: inout [String: String]) in
101108
it["message"] = "event-\(r.type ?? "unknown")"
102-
it["plugin"] = "\(plugin.type)-\(String(describing: plugin))"
103-
}
109+
if let plugin = plugin as? DestinationPlugin, !plugin.key.isEmpty {
110+
it["plugin"] = "\(plugin.type)-\(plugin.key)"
111+
} else {
112+
it["plugin"] = "\(plugin.type)-\(String(describing: plugin))"
113+
} }
104114
}
105115
}
106116

Sources/Segment/Utilities/Telemetry.swift

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public class Telemetry: Subscriber {
8989
private var seenErrors = [String: Int]()
9090
internal var started = false
9191
private var rateLimitEndTime: TimeInterval = 0
92+
internal var flushFirstError = true
9293
private var telemetryQueue = DispatchQueue(label: "telemetryQueue")
9394
private var updateQueue = DispatchQueue(label: "updateQueue")
9495
private var telemetryTimer: QueueTimer?
@@ -98,15 +99,10 @@ public class Telemetry: Subscriber {
9899
guard enable, !started, sampleRate > 0.0 && sampleRate <= 1.0 else { return }
99100
started = true
100101

102+
// Queue contents were sampled at the default 100%
103+
// the values on flush will be adjusted in the send function
101104
if Double.random(in: 0...1) > sampleRate {
102105
resetQueue()
103-
} else {
104-
telemetryQueue.async {
105-
self.queue = self.queue.map { var metric = $0
106-
metric.value = Int(Double(metric.value) / self.sampleRate)
107-
return metric
108-
}
109-
}
110106
}
111107

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

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

141-
if Double.random(in: 0...1) > sampleRate { return }
142-
143138
addRemoteMetric(metric: metric, tags: tags)
144139
}
145140

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

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

168-
if let errorKey = tags["error"] {
169-
if let count = seenErrors[errorKey] {
170-
seenErrors[errorKey] = count + 1
171-
if Double.random(in: 0...1) > sampleRate { return }
172-
addRemoteMetric(metric: metric, tags: filteredTags, value: Int(Double(count) * sampleRate), log: logData)
173-
seenErrors[errorKey] = 0
174-
} else {
175-
addRemoteMetric(metric: metric, tags: filteredTags, log: logData)
176-
flush()
177-
seenErrors[errorKey] = 0
178-
}
179-
} else {
180-
addRemoteMetric(metric: metric, tags: filteredTags, log: logData)
164+
addRemoteMetric(metric: metric, tags: filteredTags, log: logData)
165+
166+
if (flushFirstError) {
167+
flushFirstError = false
168+
flush()
181169
}
182170
}
183171

Tests/Segment-Tests/Telemetry_Tests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class TelemetryTests: XCTestCase {
116116

117117
func testHTTPException() {
118118
mockTelemetryHTTPClient(shouldThrow: true)
119+
Telemetry.shared.flushFirstError = true
119120
Telemetry.shared.enable = true
120121
Telemetry.shared.start()
121122
Telemetry.shared.error(metric: Telemetry.INVOKE_METRIC, log: "log") { $0["error"] = "test" }

0 commit comments

Comments
 (0)