Skip to content

Commit

Permalink
Refactoring out unhelpful size checks, fixing warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelGHSeg committed Nov 25, 2024
1 parent ec2a96e commit 20f6aae
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
19 changes: 4 additions & 15 deletions Sources/Segment/Utilities/Telemetry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public class Telemetry: Subscriber {

internal var queue = [RemoteMetric]()
private var queueBytes = 0
private var queueSizeExceeded = false
internal var started = false
private var rateLimitEndTime: TimeInterval = 0
internal var flushFirstError = true
Expand Down Expand Up @@ -126,7 +125,7 @@ public class Telemetry: Subscriber {
/// - metric: The metric name.
/// - 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 }
guard enable, sampleRate > 0.0 && sampleRate <= 1.0, metric.hasPrefix(Telemetry.METRICS_BASE_TAG) else { return }
if Double.random(in: 0...1) > sampleRate { return }

var tags = [String: String]()
Expand All @@ -142,7 +141,7 @@ public class Telemetry: Subscriber {
/// - log: The log data.
/// - 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 }
guard enable, sampleRate > 0.0 && sampleRate <= 1.0, metric.hasPrefix(Telemetry.METRICS_BASE_TAG) else { return }
if Double.random(in: 0...1) > sampleRate { return }

var tags = [String: String]()
Expand Down Expand Up @@ -198,7 +197,6 @@ public class Telemetry: Subscriber {
sendQueue.append(metric)
}
queueBytes = 0
queueSizeExceeded = false

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

guard queue.count < maxQueueSize else { return }

let newMetric = RemoteMetric(
type: METRIC_TYPE,
metric: metric,
Expand All @@ -264,8 +264,6 @@ public class Telemetry: Subscriber {
if queueBytes + newMetricSize <= maxQueueBytes {
queue.append(newMetric)
queueBytes += newMetricSize
} else {
queueSizeExceeded = true
}
}
}
Expand Down Expand Up @@ -295,19 +293,10 @@ public class Telemetry: Subscriber {
return request
}

private func queueHasSpace() -> Bool {
var under = false
telemetryQueue.sync {
under = queue.count < maxQueueSize
}
return under
}

private func resetQueue() {
telemetryQueue.sync {
queue.removeAll()
queueBytes = 0
queueSizeExceeded = false
}
}
}
6 changes: 3 additions & 3 deletions Tests/Segment-Tests/Telemetry_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ class TelemetryTests: XCTestCase {

func testConcurrentErrorReporting() {
Telemetry.shared.enable = true
Telemetry.shared.start()
let operationCount = 200

var concurrentExpectation = XCTestExpectation(description: "High pressure operations")
let concurrentExpectation = XCTestExpectation(description: "High pressure operations")
concurrentExpectation.expectedFulfillmentCount = operationCount

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

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

Expand Down

0 comments on commit 20f6aae

Please sign in to comment.