Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload module improvements #135

Merged
merged 6 commits into from
Dec 4, 2024
Merged

Upload module improvements #135

merged 6 commits into from
Dec 4, 2024

Conversation

ArielDemarco
Copy link
Collaborator

Overview

This PR addresses some issues and also does some improvements on the way the UploadModule works.

Details

  • Reduced the complexity of EmbraceUploadCache to clean/purge it only based on date.
  • Added exponential backoff to all retry logics (more info on ExponentialBackoff struct).
  • Fixed a bug that would prevent from retrying requests.
  • Fixed a bug that caused the attemptCount to not be properly included in the headers of retried requests
  • Improved logic to understand which requests should be retried based on status codes (429, 500-599) and errors. . Some requests (like the ones with statusCode 400: Bad Request or the ones with specific errors like URLError.badURL) are going to be dropped.
  • Some improvements were made to the request recovery and retry process (which occurs after a request fails multiple times and is stored in the cache):
    • Queue the different retry operations (up to two) to avoid overwhelming the backend
    • Prevents this process from being executed multiple times (it'll wait until the first "retry" of all requests finishes)
    • maximumAmountOfRetries was added to RedundancyOptions to prevent requests from retrying indefinitely (which fixes a bug that would use an unnecessary amount of memory and data).
  • Added the possibility to use the Retry-After header in combination with the Expontential Backoff logic.

@ArielDemarco ArielDemarco requested a review from a team as a code owner December 4, 2024 13:48
Copy link

github-actions bot commented Dec 4, 2024

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

OpenSSF Scorecard

PackageVersionScoreDetails

Scanned Files

@@ -54,25 +63,46 @@ public class EmbraceUpload: EmbraceLogUploader {
/// Attempts to upload all the available cached data.
public func retryCachedData() {
queue.async { [weak self] in
guard let self = self else { return }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ⚠️ Conditional statements should always return on the next line (conditional_returns_on_newline)


// clear data from cache that shouldn't be retried as it's stale
self.clearCacheFromStaleData()

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ⚠️ Lines should not have trailing whitespace (trailing_whitespace)

// retry for all other non-handled cases with errors
return error != nil
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ⚠️ Lines should not have trailing whitespace (trailing_whitespace)

maxDelay: Double = 32.0
) {
self.baseDelay = baseDelay
self.maxDelay = max(maxDelay, baseDelay) // prevent somebody from adding a baseDelay larger than the maxDelay
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ⚠️ Line should be 120 characters or less; currently it has 121 characters (line_length)

@@ -44,7 +44,7 @@ final class SessionControllerTests: XCTestCase {
)

self.queue = DispatchQueue(label: "com.test.embrace.upload.queue", attributes: .concurrent)
upload = try EmbraceUpload(options: uploadTestOptions, logger: MockLogger(), queue: queue)
upload = try EmbraceUpload(options: uploadTestOptions, logger: MockLogger(), queue: queue, semaphore: .init(value: .max))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ⚠️ Line should be 120 characters or less; currently it has 129 characters (line_length)

@@ -729,7 +729,7 @@
let storage = try EmbraceStorage.createInMemoryDb()
defer { try? storage.teardown() }

let upload = try EmbraceUpload(options: uploadOptions, logger: logger, queue: queue)
let upload = try EmbraceUpload(options: uploadOptions, logger: logger, queue: queue, semaphore: .init(value: .max))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ⚠️ Line should be 120 characters or less; currently it has 123 characters (line_length)

}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ⚠️ Files should have a single trailing newline (trailing_newline)

@@ -33,7 +33,7 @@ class EmbraceUploadTests: XCTestCase {
)

self.queue = DispatchQueue(label: "com.test.embrace.queue", attributes: .concurrent)
module = try EmbraceUpload(options: testOptions, logger: MockLogger(), queue: queue)
module = try EmbraceUpload(options: testOptions, logger: MockLogger(), queue: queue, semaphore: .init(value: .max))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ⚠️ Line should be 120 characters or less; currently it has 123 characters (line_length)

EmbraceHTTPMock.mock(url: testSpansUrl())
EmbraceHTTPMock.mock(url: testLogsUrl())


Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ⚠️ Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)

@@ -131,28 +151,39 @@ public struct MockResponse {
private(set) var data: Data?
private(set) var error: Error?
private(set) var statusCode: Int = -1
private(set) var headers: [String: String]? = nil
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ⚠️ Initializing an optional variable with nil is redundant (redundant_optional_initialization)

Copy link

github-actions bot commented Dec 4, 2024

Warnings
⚠️ No CHANGELOG entry added.

Generated by 🚫 Danger Swift against afa7cc5

Copy link

codecov bot commented Dec 4, 2024

Codecov Report

Attention: Patch coverage is 96.22642% with 16 lines in your changes missing coverage. Please review.

Project coverage is 90.51%. Comparing base (71be298) to head (afa7cc5).
Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
...oadInternalTests/EmbraceUploadOperationTests.swift 96.20% 8 Missing ⚠️
Sources/EmbraceUploadInternal/EmbraceUpload.swift 95.78% 4 Missing ⚠️
...adInternal/Operations/EmbraceUploadOperation.swift 97.14% 2 Missing ⚠️
Tests/TestSupport/EmbraceHTTPMock.swift 89.47% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #135      +/-   ##
==========================================
- Coverage   91.94%   90.51%   -1.44%     
==========================================
  Files         410      418       +8     
  Lines       26631    27768    +1137     
==========================================
+ Hits        24487    25135     +648     
- Misses       2144     2633     +489     
Files with missing lines Coverage Δ
...braceUploadInternal/Cache/EmbraceUploadCache.swift 76.43% <100.00%> (-3.36%) ⬇️
...dInternal/Options/EmbraceUpload+CacheOptions.swift 50.00% <ø> (-2.50%) ⬇️
...nal/Options/EmbraceUpload+ExponentialBackoff.swift 100.00% <100.00%> (ø)
...rnal/Options/EmbraceUpload+RedundancyOptions.swift 100.00% <100.00%> (ø)
...raceCoreTests/Session/SessionControllerTests.swift 99.39% <100.00%> (ø)
...raceCoreTests/Session/UnsentDataHandlerTests.swift 99.59% <100.00%> (ø)
...lTests/EmbraceUploadCacheTests+ClearDataDate.swift 100.00% <100.00%> (ø)
...mbraceUploadInternalTests/EmbraceUploadTests.swift 96.81% <100.00%> (+0.05%) ⬆️
...adInternal/Operations/EmbraceUploadOperation.swift 98.62% <97.14%> (-1.38%) ⬇️
Tests/TestSupport/EmbraceHTTPMock.swift 93.96% <89.47%> (+6.57%) ⬆️
... and 2 more

... and 22 files with indirect coverage changes

@ArielDemarco
Copy link
Collaborator Author

All warnings are from Tests. We should remove that :)
Also... Run Tests seems to have worked properly. Don't know what happened with Run Danger that failed (while running tests).
Merging as everything seems fine

@ArielDemarco ArielDemarco merged commit 7ae30ac into main Dec 4, 2024
9 of 10 checks passed
@ArielDemarco ArielDemarco deleted the upload-module-improvements branch December 4, 2024 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants