Skip to content

Commit aa1b8d9

Browse files
committed
RUM-8042 Add count of batches blocked
1 parent 5832780 commit aa1b8d9

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

DatadogCore/Sources/Core/Upload/DataUploadWorker.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,23 @@ internal class DataUploadWorker: DataUploadWorkerType {
7373
let context = contextProvider.read()
7474
let blockersForUpload = uploadConditions.blockersForUpload(with: context)
7575
let isSystemReady = blockersForUpload.isEmpty
76-
let files = isSystemReady ? fileReader.readFiles(limit: maxBatchesPerUpload) : nil
77-
if let files = files, !files.isEmpty {
76+
let files = fileReader.readFiles(limit: maxBatchesPerUpload)
77+
78+
if !files.isEmpty && isSystemReady {
7879
DD.logger.debug("⏳ (\(self.featureName)) Uploading batches...")
7980
self.backgroundTaskCoordinator?.beginBackgroundTask()
8081
self.uploadFile(from: files.reversed(), context: context)
8182
sendUploadCycleMetric()
8283
} else {
83-
let batchLabel = files?.isEmpty == false ? "YES" : (isSystemReady ? "NO" : "NOT CHECKED")
84+
let batchLabel = files.isEmpty ? "NO" : "YES"
8485
DD.logger.debug("💡 (\(self.featureName)) No upload. Batch to upload: \(batchLabel), System conditions: \(blockersForUpload.description)")
8586
self.delay.increase()
8687
self.backgroundTaskCoordinator?.endBackgroundTask()
8788
self.scheduleNextCycle()
88-
sendBatchBlockedMetric(blockers: blockersForUpload)
89+
sendBatchBlockedMetric(blockers: blockersForUpload, batchCount: files.count)
8990
}
9091
}
92+
9193
self.readWork = readWorkItem
9294

9395
// Start sending batches immediately after initialization:
@@ -127,6 +129,7 @@ internal class DataUploadWorker: DataUploadWorkerType {
127129
DD.logger.debug(" → (\(self.featureName)) not delivered, will be retransmitted: \(uploadStatus.userDebugDescription)")
128130
self.delay.increase()
129131
self.scheduleNextCycle()
132+
sendBatchBlockedMetric(status: uploadStatus, batchCount: files.count)
130133
return
131134
}
132135

@@ -143,7 +146,6 @@ internal class DataUploadWorker: DataUploadWorkerType {
143146
previousUploadStatus = nil
144147

145148
if let error = uploadStatus.error {
146-
sendBatchBlockedMetric(error: error)
147149
throw error // Throw to report the request error accordingly
148150
}
149151
} catch DataUploadError.httpError(statusCode: .unauthorized), DataUploadError.httpError(statusCode: .forbidden) {
@@ -237,7 +239,7 @@ internal class DataUploadWorker: DataUploadWorkerType {
237239
)
238240
}
239241

240-
private func sendBatchBlockedMetric(blockers: [DataUploadConditions.Blocker]) {
242+
private func sendBatchBlockedMetric(blockers: [DataUploadConditions.Blocker], batchCount: Int) {
241243
guard !blockers.isEmpty else {
242244
return
243245
}
@@ -248,6 +250,7 @@ internal class DataUploadWorker: DataUploadWorkerType {
248250
SDKMetricFields.typeKey: BatchBlockedMetric.typeValue,
249251
BatchMetric.trackKey: featureName,
250252
BatchBlockedMetric.uploaderDelayKey: delay.current,
253+
BatchBlockedMetric.batchCount: batchCount,
251254
BatchBlockedMetric.blockers: blockers.map {
252255
switch $0 {
253256
case .battery: return "low_battery"
@@ -260,13 +263,18 @@ internal class DataUploadWorker: DataUploadWorkerType {
260263
)
261264
}
262265

263-
private func sendBatchBlockedMetric(error: DataUploadError) {
266+
private func sendBatchBlockedMetric(status: DataUploadStatus, batchCount: Int) {
267+
guard let error = status.error else {
268+
return
269+
}
270+
264271
telemetry.metric(
265272
name: BatchBlockedMetric.name,
266273
attributes: [
267274
SDKMetricFields.typeKey: BatchBlockedMetric.typeValue,
268275
BatchMetric.trackKey: featureName,
269276
BatchBlockedMetric.uploaderDelayKey: delay.current,
277+
BatchBlockedMetric.batchCount: batchCount,
270278
BatchBlockedMetric.failure: {
271279
switch error {
272280
case let .httpError(code): return "intake-code-\(code.rawValue)"

DatadogCore/Sources/SDKMetrics/BatchMetrics.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ internal enum BatchBlockedMetric {
142142
/// It is applied in addition to the telemetry sample rate (20% by default).
143143
static let sampleRate: Float = 1.5 // 1.5%
144144
/// The key for uploader's current delay.
145-
static let uploaderDelayKey = "uploader_delay"
145+
static let uploaderDelayKey = "uploader_delay.current"
146+
/// The key for count of bacthes being blocked.
147+
static let batchCount = "batch_count"
146148

147149
/// List of upload blockers
148150
static let blockers = "blockers"

0 commit comments

Comments
 (0)