Skip to content

Commit 20f6aae

Browse files
committed
Refactoring out unhelpful size checks, fixing warnings
1 parent ec2a96e commit 20f6aae

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

Sources/Segment/Utilities/Telemetry.swift

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ public class Telemetry: Subscriber {
8585

8686
internal var queue = [RemoteMetric]()
8787
private var queueBytes = 0
88-
private var queueSizeExceeded = false
8988
internal var started = false
9089
private var rateLimitEndTime: TimeInterval = 0
9190
internal var flushFirstError = true
@@ -126,7 +125,7 @@ public class Telemetry: Subscriber {
126125
/// - metric: The metric name.
127126
/// - buildTags: A closure to build the tags dictionary.
128127
func increment(metric: String, buildTags: (inout [String: String]) -> Void) {
129-
guard enable, sampleRate > 0.0 && sampleRate <= 1.0, metric.hasPrefix(Telemetry.METRICS_BASE_TAG), queueHasSpace() else { return }
128+
guard enable, sampleRate > 0.0 && sampleRate <= 1.0, metric.hasPrefix(Telemetry.METRICS_BASE_TAG) else { return }
130129
if Double.random(in: 0...1) > sampleRate { return }
131130

132131
var tags = [String: String]()
@@ -142,7 +141,7 @@ public class Telemetry: Subscriber {
142141
/// - log: The log data.
143142
/// - buildTags: A closure to build the tags dictionary.
144143
func error(metric: String, log: String, buildTags: (inout [String: String]) -> Void) {
145-
guard enable, sampleRate > 0.0 && sampleRate <= 1.0, metric.hasPrefix(Telemetry.METRICS_BASE_TAG), queueHasSpace() else { return }
144+
guard enable, sampleRate > 0.0 && sampleRate <= 1.0, metric.hasPrefix(Telemetry.METRICS_BASE_TAG) else { return }
146145
if Double.random(in: 0...1) > sampleRate { return }
147146

148147
var tags = [String: String]()
@@ -198,7 +197,6 @@ public class Telemetry: Subscriber {
198197
sendQueue.append(metric)
199198
}
200199
queueBytes = 0
201-
queueSizeExceeded = false
202200

203201
let payload = try JSONEncoder().encode(["series": sendQueue])
204202
var request = upload(apiHost: host)
@@ -253,6 +251,8 @@ public class Telemetry: Subscriber {
253251
return
254252
}
255253

254+
guard queue.count < maxQueueSize else { return }
255+
256256
let newMetric = RemoteMetric(
257257
type: METRIC_TYPE,
258258
metric: metric,
@@ -264,8 +264,6 @@ public class Telemetry: Subscriber {
264264
if queueBytes + newMetricSize <= maxQueueBytes {
265265
queue.append(newMetric)
266266
queueBytes += newMetricSize
267-
} else {
268-
queueSizeExceeded = true
269267
}
270268
}
271269
}
@@ -295,19 +293,10 @@ public class Telemetry: Subscriber {
295293
return request
296294
}
297295

298-
private func queueHasSpace() -> Bool {
299-
var under = false
300-
telemetryQueue.sync {
301-
under = queue.count < maxQueueSize
302-
}
303-
return under
304-
}
305-
306296
private func resetQueue() {
307297
telemetryQueue.sync {
308298
queue.removeAll()
309299
queueBytes = 0
310-
queueSizeExceeded = false
311300
}
312301
}
313302
}

Tests/Segment-Tests/Telemetry_Tests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,9 @@ class TelemetryTests: XCTestCase {
146146

147147
func testConcurrentErrorReporting() {
148148
Telemetry.shared.enable = true
149-
Telemetry.shared.start()
150149
let operationCount = 200
151150

152-
var concurrentExpectation = XCTestExpectation(description: "High pressure operations")
151+
let concurrentExpectation = XCTestExpectation(description: "High pressure operations")
153152
concurrentExpectation.expectedFulfillmentCount = operationCount
154153

155154
// Use multiple dispatch queues to increase concurrency
@@ -187,12 +186,13 @@ class URLSessionMock: RestrictedHTTPSession {
187186
var shouldThrow = false
188187

189188
override func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
189+
let task = URLSession.shared.dataTask(with: request) { _, _, _ in }
190190
if shouldThrow {
191191
completionHandler(nil, nil, NSError(domain: "Test", code: 1, userInfo: nil))
192192
} else {
193193
completionHandler(nil, HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: nil), nil)
194194
}
195-
return URLSessionDataTaskMock()
195+
return task
196196
}
197197
}
198198

0 commit comments

Comments
 (0)