Skip to content

Commit

Permalink
Updating logic
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoEmbrace committed Jan 17, 2025
1 parent 92d8c57 commit c010b84
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 64 deletions.
21 changes: 4 additions & 17 deletions Sources/EmbraceCore/Internal/Logs/LogController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ class LogController: LogControllable {
.addSessionIdentifier()
.build()

var send = true

// handle attachment data
if let attachment = attachment {

sessionController.increaseAttachmentCount()

let id = UUID().withoutHyphen
finalAttributes[LogSemantics.keyAttachmentId] = id

Expand All @@ -134,18 +134,7 @@ class LogController: LogControllable {

// upload attachment
else {
send = false

upload?.uploadAttachment(id: id, data: attachment, completion: { [weak self] result in
switch result {
case .success:
self?.sessionController?.increaseAttachmentCount()
case .failure:
finalAttributes[LogSemantics.keyAttachmentErrorCode] = LogSemantics.attachmentFailedUpload
}

self?.otel.log(message, severity: severity, timestamp: timestamp, attributes: finalAttributes)
})
upload?.uploadAttachment(id: id, data: attachment, completion: nil)
}
}

Expand All @@ -161,9 +150,7 @@ class LogController: LogControllable {
}
}

if send {
otel.log(message, severity: severity, timestamp: timestamp, attributes: finalAttributes)
}
otel.log(message, severity: severity, timestamp: timestamp, attributes: finalAttributes)
}
}

Expand Down
1 change: 0 additions & 1 deletion Sources/EmbraceSemantics/Logs/LogSemantics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ public struct LogSemantics {
public static let keyAttachmentErrorCode = "emb.attachment_error_code"

public static let attachmentTooLarge = "ATTACHMENT_TOO_LARGE"
public static let attachmentFailedUpload = "UNSUCCESSFUL_UPLOAD"
public static let attachmentLimitReached = "OVER_MAX_ATTACHMENTS"
}
56 changes: 20 additions & 36 deletions Sources/EmbraceUploadInternal/EmbraceUpload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ public class EmbraceUpload: EmbraceLogUploader {
data: Data,
type: EmbraceUploadType,
attemptCount: Int = 0,
completion: ((Result<(), Error>) -> Void)?) {
completion: ((Result<(), Error>) -> Void)?
) {

// validate identifier
guard id.isEmpty == false else {
completion?(.failure(EmbraceUploadError.internalError(.invalidMetadata)))
Expand All @@ -176,59 +178,41 @@ public class EmbraceUpload: EmbraceLogUploader {
}

// cache operation
var cacheOperation: BlockOperation?

if type != .attachment {
cacheOperation = BlockOperation { [weak self] in
do {
try self?.cache.saveUploadData(id: id, type: type, data: data)
completion?(.success(()))
} catch {
self?.logger.debug("Error caching upload data: \(error.localizedDescription)")
completion?(.failure(error))
}
let cacheOperation = BlockOperation { [weak self] in
do {
try self?.cache.saveUploadData(id: id, type: type, data: data)
completion?(.success(()))
} catch {
self?.logger.debug("Error caching upload data: \(error.localizedDescription)")
completion?(.failure(error))
}
}

// upload operation
let retryCount = type != .attachment ?
options.redundancy.automaticRetryCount :
options.redundancy.attachmentRetryCount

let uploadOperation = createUploadOperation(
id: id,
type: type,
urlSession: urlSession,
data: data,
retryCount: retryCount,
retryCount: options.redundancy.automaticRetryCount,
attemptCount: attemptCount) { [weak self] (result, attemptCount) in

self?.queue.async { [weak self] in

if type == .attachment {
switch result {
case .success: completion?(.success(()))
case .failure: completion?(.failure(EmbraceUploadError.internalError(.attachmentUploadFailed)))
}
} else {
self?.handleOperationFinished(
id: id,
type: type,
result: result,
attemptCount: attemptCount
)
self?.handleOperationFinished(
id: id,
type: type,
result: result,
attemptCount: attemptCount
)

self?.clearCacheFromStaleData()
}
self?.clearCacheFromStaleData()
}
}

// queue operations
if let cacheOperation = cacheOperation {
uploadOperation.addDependency(cacheOperation)
operationQueue.addOperation(cacheOperation)
}

uploadOperation.addDependency(cacheOperation)
operationQueue.addOperation(cacheOperation)
operationQueue.addOperation(uploadOperation)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public enum EmbraceUploadErrorCode: Int {
case invalidMetadata = 1000
case invalidData = 1001
case operationCancelled = 1002
case attachmentUploadFailed = 1003
}

public enum EmbraceUploadError: Error, Equatable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,18 @@ public extension EmbraceUpload {
/// Enable to automatically try to send any unsent cached data when the phone regains internet connection.
public let retryOnInternetConnected: Bool

/// The amount of times an attachment upload can be retried
public let attachmentRetryCount: Int

/// Defines the behavior to use when retrying requests
public let exponentialBackoffBehavior: ExponentialBackoff

public init(
automaticRetryCount: Int = 3,
maximumAmountOfRetries: Int = 20,
retryOnInternetConnected: Bool = true,
attachmentRetryCount: Int = 3,
exponentialBackoffBehavior: ExponentialBackoff = .init()
) {
self.automaticRetryCount = automaticRetryCount
self.maximumAmountOfRetries = maximumAmountOfRetries
self.retryOnInternetConnected = retryOnInternetConnected
self.attachmentRetryCount = attachmentRetryCount
self.exponentialBackoffBehavior = exponentialBackoffBehavior
}
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/EmbraceCoreTests/Internal/Logs/LogControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class LogControllerTests: XCTestCase {
givenFailingLogUploader()
givenLogController()
whenCreatingLogWithAttachment()
thenLogWithUnsuccessfulAttachmentIsCreatedCorrectly(errorCode: "UNSUCCESSFUL_UPLOAD")
thenLogWithUnsuccessfulAttachmentIsCreatedCorrectly(errorCode: nil)
}

func test_createLogWithPreuploadedAttachment() throws {
Expand Down Expand Up @@ -431,13 +431,13 @@ private extension LogControllerTests {
}
}

func thenLogWithUnsuccessfulAttachmentIsCreatedCorrectly(errorCode: String) {
func thenLogWithUnsuccessfulAttachmentIsCreatedCorrectly(errorCode: String?) {
wait {
let log = self.otelBridge.otel.logs.first

let attachmentIdFound = log!.attributes["emb.attachment_id"] != nil
let attachmentSizeFound = log!.attributes["emb.attachment_size"] != nil
let attachmentErrorFound = log!.attributes["emb.attachment_error_code"]!.description == errorCode
let attachmentErrorFound = errorCode == nil || log!.attributes["emb.attachment_error_code"]!.description == errorCode

return attachmentIdFound && attachmentSizeFound && attachmentErrorFound
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/EmbraceUploadInternalTests/EmbraceUploadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class EmbraceUploadTests: XCTestCase {

func test_attachmentsEndpoint() throws {
// when uploading attachment data
module.uploadLog(id: "id", data: TestConstants.data, completion: nil)
module.uploadAttachment(id: "id", data: TestConstants.data, completion: nil)

wait(delay: .defaultTimeout)

Expand Down

0 comments on commit c010b84

Please sign in to comment.