Skip to content

Commit 5de4626

Browse files
Apply CR suggestions
1 parent 59d31c6 commit 5de4626

3 files changed

Lines changed: 13 additions & 15 deletions

File tree

DatadogRUM/Sources/Feature/RUMFeature.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ internal final class RUMFeature: DatadogRemoteFeature, RUMSessionSamplerProvider
140140
memoryReader: $0.memory,
141141
featureScope: featureScope,
142142
batchSize: configuration.timeseriesBatchSize,
143-
collectInBackground: configuration.trackBackgroundEvents,
144143
ciTest: ciTest,
145144
syntheticsTest: syntheticsTest,
146145
sessionSampleRate: Double(sessionSampleRate)

DatadogRUM/Sources/Timeseries/TimeseriesSessionCollector.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ internal class TimeseriesSessionCollector: TimeseriesCollecting {
4343
private let cpuUsageProvider: () -> Double?
4444
private let batchSize: Int
4545
private let samplingInterval: TimeInterval
46-
private let collectInBackground: Bool
4746
private let featureScope: FeatureScope
4847
private let totalRAM: Double
4948
private let ciTest: RUMCITest?
@@ -71,7 +70,6 @@ internal class TimeseriesSessionCollector: TimeseriesCollecting {
7170
featureScope: FeatureScope,
7271
batchSize: Int = 120,
7372
samplingInterval: TimeInterval = 1,
74-
collectInBackground: Bool = false,
7573
cpuUsageProvider: (() -> Double?)? = nil,
7674
totalRAM: Double = Double(ProcessInfo.processInfo.physicalMemory),
7775
ciTest: RUMCITest? = nil,
@@ -81,7 +79,6 @@ internal class TimeseriesSessionCollector: TimeseriesCollecting {
8179
self.memoryReader = memoryReader
8280
self.batchSize = max(2, batchSize)
8381
self.samplingInterval = samplingInterval
84-
self.collectInBackground = collectInBackground
8582
self.featureScope = featureScope
8683
self.totalRAM = totalRAM
8784
self.ciTest = ciTest
@@ -90,6 +87,10 @@ internal class TimeseriesSessionCollector: TimeseriesCollecting {
9087
self.cpuUsageProvider = cpuUsageProvider ?? { TimeseriesSessionCollector.processCPU() }
9188
}
9289

90+
deinit {
91+
timer?.cancel()
92+
}
93+
9394
/// Per-process CPU as a percentage (0–100+), summed across all app threads.
9495
/// Separated into a static so it can be called from the init closure without capturing self.
9596
private static func processCPU() -> Double? {
@@ -121,7 +122,7 @@ internal class TimeseriesSessionCollector: TimeseriesCollecting {
121122
var info = thread_basic_info()
122123
var infoCount = mach_msg_type_number_t(THREAD_INFO_MAX)
123124
let kr = withUnsafeMutablePointer(to: &info) {
124-
$0.withMemoryRebound(to: integer_t.self, capacity: 1) {
125+
$0.withMemoryRebound(to: integer_t.self, capacity: Int(infoCount)) {
125126
thread_info(threadsList[Int(i)], thread_flavor_t(THREAD_BASIC_INFO), $0, &infoCount)
126127
}
127128
}
@@ -130,7 +131,7 @@ internal class TimeseriesSessionCollector: TimeseriesCollecting {
130131
}
131132
total += Double(info.cpu_usage) / Double(TH_USAGE_SCALE) * 100.0
132133
}
133-
return total
134+
return min(total, 100.0)
134135
#endif
135136
}
136137

@@ -155,13 +156,12 @@ internal class TimeseriesSessionCollector: TimeseriesCollecting {
155156
}
156157

157158
/// Suspends sampling and flushes buffered data. Session state is preserved for `resume()`. Idempotent.
158-
/// No-op when `collectInBackground` is `true`.
159159
func pause() {
160160
queue.async { [weak self] in
161161
guard let self = self else {
162162
return
163163
}
164-
if self.collectInBackground || self.isPaused || self.timer == nil {
164+
if self.isPaused || self.timer == nil {
165165
return
166166
}
167167
self.timer?.cancel()

DatadogRUM/Tests/Timeseries/TimeseriesSessionCollectorTests.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -627,15 +627,14 @@ class TimeseriesSessionCollectorTests: XCTestCase {
627627
XCTAssertGreaterThan(countAfterResume, countAfterPause, "Expected new events after resume")
628628
}
629629

630-
func testWhenCollectInBackgroundEnabled_pauseIsNoOp() {
630+
func testWhenBackgrounded_pauseAlwaysStopsSampling() {
631631
// Given
632632
memoryReader.vitalData = 1_000_000
633633
let collector = TimeseriesSessionCollector(
634634
memoryReader: memoryReader,
635635
featureScope: featureScope,
636636
batchSize: 2,
637637
samplingInterval: 0.05,
638-
collectInBackground: true,
639638
cpuUsageProvider: { nil }
640639
)
641640
let contextReader = RUMActiveContextReaderMock()
@@ -651,17 +650,17 @@ class TimeseriesSessionCollectorTests: XCTestCase {
651650
let countBeforePause = featureScope.eventsWritten(ofType: RUMTimeseriesMemoryEvent.self).count
652651
XCTAssertGreaterThan(countBeforePause, 0)
653652

654-
// When — pause should be a no-op
655-
let afterPauseExpectation = self.expectation(description: "sampling continues after pause")
653+
// When — pause on backgrounding
654+
let afterPauseExpectation = self.expectation(description: "sampling stopped after pause")
656655
afterPauseExpectation.assertForOverFulfill = false
657656
collector.pause()
658657
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { afterPauseExpectation.fulfill() }
659658
waitForExpectations(timeout: 2)
660-
collector.stop()
661659

662-
// Then — events keep accumulating
660+
// Then — no new events accumulate while paused
663661
let countAfterPause = featureScope.eventsWritten(ofType: RUMTimeseriesMemoryEvent.self).count
664-
XCTAssertGreaterThan(countAfterPause, countBeforePause, "Sampling should continue when collectInBackground = true")
662+
XCTAssertEqual(countAfterPause, countBeforePause, "Sampling should stop while backgrounded, regardless of trackBackgroundEvents")
663+
collector.stop()
665664
}
666665

667666
func testWhenPauseCalledBeforeStart_itIsNoOp() {

0 commit comments

Comments
 (0)