Skip to content

Commit

Permalink
Fixing log batches logic
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoEmbrace committed Nov 1, 2024
1 parent b2da5ac commit b982301
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
29 changes: 17 additions & 12 deletions Sources/EmbraceCore/Internal/Logs/Exporter/DefaultLogBatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ protocol LogBatcherDelegate: AnyObject {
protocol LogBatcher {
func addLogRecord(logRecord: LogRecord)
func renewBatch(withLogs logRecords: [LogRecord])
func forceEndCurrentBatch()
}

class DefaultLogBatcher: LogBatcher {
Expand Down Expand Up @@ -52,20 +53,24 @@ class DefaultLogBatcher: LogBatcher {
}

internal extension DefaultLogBatcher {
func renewBatch(withLogs logRecords: [LogRecord] = []) {
func forceEndCurrentBatch() {
processorQueue.async {
guard let batch = self.batch else {
return
}
self.cancelBatchDeadline()
self.delegate?.batchFinished(withLogs: batch.logs)
// TODO: Add cleanup step:
// --> delete reported logs
self.batch = .init(limits: self.logLimits, logs: logRecords)
self.renewBatch()
}
}

if logRecords.count > 0 {
self.renewBatchDeadline(with: self.logLimits)
}
func renewBatch(withLogs logRecords: [LogRecord] = []) {
guard let batch = self.batch else {
return
}
self.cancelBatchDeadline()
self.delegate?.batchFinished(withLogs: batch.logs)
// TODO: Add cleanup step:
// --> delete reported logs
self.batch = .init(limits: self.logLimits, logs: logRecords)

if logRecords.count > 0 {
self.renewBatchDeadline(with: self.logLimits)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class StorageEmbraceLogExporter: LogRecordExporter {

@objc func onSessionEnd(noticication: Notification) {
// forcefully start a new batch of logs when a session ends
logBatcher.renewBatch(withLogs: [])
logBatcher.forceEndCurrentBatch()
}

func export(logRecords: [ReadableLogRecord], explicitTimeout: TimeInterval?) -> ExportResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ class SpyLogBatcher: LogBatcher {
logRecords.append(logRecord)
}

func forceEndCurrentBatch() {
self.renewBatch(withLogs: [])
}

private(set) var didCallRenewBatch: Bool = false
private(set) var renewBatchInvocationCount: Int = 0
func renewBatch(withLogs logRecords: [LogRecord]) {
Expand Down

0 comments on commit b982301

Please sign in to comment.