From d71cffed38cccda2af59b08a0b3cadb4f3887e9a Mon Sep 17 00:00:00 2001 From: Hongyan Jiang Date: Wed, 21 Aug 2024 23:43:23 -0400 Subject: [PATCH] Do not rate limit session start beacon and crash beacons --- Changelog.md | 4 ++ InstanaAgent.podspec | 2 +- Sources/InstanaAgent/Beacons/Reporter.swift | 2 +- .../Beacons/ReporterRateLimiter.swift | 8 ++- .../InstanaAgent/Configuration/Defines.swift | 2 +- .../Configuration/VersionConfig.swift | 2 +- .../Beacons/ReporterRateLimitTests.swift | 53 +++++++++++++++---- .../InstanaSystemUtilsTests.swift | 2 +- 8 files changed, 57 insertions(+), 18 deletions(-) diff --git a/Changelog.md b/Changelog.md index 926d8b5a..22f13f9e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,9 @@ # Changelog +## 1.8.3 +- do not rate limit session start beacon and crash beacons +- fix typo of crash exception code + ## 1.8.2 - crash beacon errorType value update - When http request failed, track statusCode along with error message diff --git a/InstanaAgent.podspec b/InstanaAgent.podspec index f63cc0bd..f8f450fc 100644 --- a/InstanaAgent.podspec +++ b/InstanaAgent.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |s| # s.name = "InstanaAgent" - s.version = "1.8.2" + s.version = "1.8.3" s.summary = "Instana iOS agent." # This description is used to generate tags and improve search results. diff --git a/Sources/InstanaAgent/Beacons/Reporter.swift b/Sources/InstanaAgent/Beacons/Reporter.swift index f960e33f..5878b22a 100644 --- a/Sources/InstanaAgent/Beacons/Reporter.swift +++ b/Sources/InstanaAgent/Beacons/Reporter.swift @@ -66,7 +66,7 @@ public class Reporter { completion?(false) return } - guard self.rateLimiter.canSubmit() else { + guard self.rateLimiter.canSubmit(beacon) else { self.session.logger.add("Rate Limit reached - Beacon might be discarded", level: .warning) completion?(false) return diff --git a/Sources/InstanaAgent/Beacons/ReporterRateLimiter.swift b/Sources/InstanaAgent/Beacons/ReporterRateLimiter.swift index a8de2369..5f7af978 100644 --- a/Sources/InstanaAgent/Beacons/ReporterRateLimiter.swift +++ b/Sources/InstanaAgent/Beacons/ReporterRateLimiter.swift @@ -44,7 +44,11 @@ class ReporterRateLimiter { limiters = configs.map { Limiter(maxItems: $0.maxItems, timeout: $0.timeout) } } - func canSubmit() -> Bool { - limiters.map { $0.signal() }.filter { $0 == true }.isEmpty + func canSubmit(_ beacon: Beacon) -> Bool { + if beacon is SessionProfileBeacon || beacon is DiagnosticBeacon { + // DiagnosticBeacon and SessionProfileBeacon can always be submitted + return true + } + return limiters.map { $0.signal() }.filter { $0 == true }.isEmpty } } diff --git a/Sources/InstanaAgent/Configuration/Defines.swift b/Sources/InstanaAgent/Configuration/Defines.swift index 931c6da8..1a3375b6 100644 --- a/Sources/InstanaAgent/Configuration/Defines.swift +++ b/Sources/InstanaAgent/Configuration/Defines.swift @@ -44,7 +44,7 @@ let crashMetaKeyUserID = "ui" let crashMetaKeyUserName = "un" let crashMetaKeyUserEmail = "ue" -let currentInstanaCrashPayloadVersion = "0.95" +let currentInstanaCrashPayloadVersion = "0.96" let defaultCrashViewName = "CrashView" let maxSecondsToKeepCrashLog = (maxDaysToKeepCrashLog * 60 * 60 * 24) diff --git a/Sources/InstanaAgent/Configuration/VersionConfig.swift b/Sources/InstanaAgent/Configuration/VersionConfig.swift index e172536b..fd2efc8e 100644 --- a/Sources/InstanaAgent/Configuration/VersionConfig.swift +++ b/Sources/InstanaAgent/Configuration/VersionConfig.swift @@ -1,3 +1,3 @@ struct VersionConfig { - static let agentVersion = "1.8.2" + static let agentVersion = "1.8.3" } diff --git a/Tests/InstanaAgentTests/Beacons/ReporterRateLimitTests.swift b/Tests/InstanaAgentTests/Beacons/ReporterRateLimitTests.swift index 475b7374..58aa0da7 100644 --- a/Tests/InstanaAgentTests/Beacons/ReporterRateLimitTests.swift +++ b/Tests/InstanaAgentTests/Beacons/ReporterRateLimitTests.swift @@ -3,6 +3,30 @@ import XCTest import Network class ReporterRateLimitTests: InstanaTestCase { + private let sessionBeacon = SessionProfileBeacon(state: .start) + private let crashBeacon = DiagnosticBeacon(crashSession: + PreviousSession(id: UUID(), + startTime: Calendar.current.date(byAdding: .minute, value: -10, to: Date())!, + viewName: "mockViewName", + carrier: "mockCarrier", + connectionType: "mockConnectionType", + userID: "mockUserID", + userEmail: "mockEmail", + userName: "mockUserName"), + crashGroupID: UUID(), + crashType: .crash, + crashTime: Calendar.current.date(byAdding: .minute, value: -5, to: Date())!.millisecondsSince1970, + duration: 0, + crashPayload: "", + formatted: "", + errorType: "", + errorMessage: "", + isSymbolicated: false) + // following beacons are rate limited + private let httpBeacon = HTTPBeacon(method: "GET", url:URL(string: "https://www.ibm.com")!, responseCode: 200) + private let customBeacon = CustomBeacon(name: "TestCustomBeacon1") + private let viewChangeBeacon = ViewChange(viewName: "TestView1") + private let alertBeacon = AlertBeacon(alertType: .lowMemory) func test_rateLimitReached_one() { // Given @@ -10,7 +34,7 @@ class ReporterRateLimitTests: InstanaTestCase { let limiter = ReporterRateLimiter(configs: configs) // When - let result = limiter.canSubmit() + let result = limiter.canSubmit(customBeacon) // Then XCTAssertTrue(result) @@ -22,11 +46,18 @@ class ReporterRateLimitTests: InstanaTestCase { let limiter = ReporterRateLimiter(configs: configs) // When - var result = limiter.canSubmit() - result = limiter.canSubmit() + var result = limiter.canSubmit(customBeacon) + result = limiter.canSubmit(customBeacon) // Then XCTAssertFalse(result) + + // More + XCTAssertFalse(limiter.canSubmit(httpBeacon)) + XCTAssertFalse(limiter.canSubmit(viewChangeBeacon)) + XCTAssertFalse(limiter.canSubmit(alertBeacon)) + XCTAssertTrue(limiter.canSubmit(sessionBeacon)) + XCTAssertTrue(limiter.canSubmit(crashBeacon)) } func test_rateLimitReached_zero_allowed() { @@ -35,7 +66,7 @@ class ReporterRateLimitTests: InstanaTestCase { let limiter = ReporterRateLimiter(configs: configs) // When - let result = limiter.canSubmit() + let result = limiter.canSubmit(customBeacon) // Then XCTAssertFalse(result) @@ -50,10 +81,10 @@ class ReporterRateLimitTests: InstanaTestCase { let limiter = ReporterRateLimiter(configs: configs) // When - var result = limiter.canSubmit() + var result = limiter.canSubmit(customBeacon) // Fire another one AFTER the first limit has been cleared DispatchQueue.main.asyncAfter(deadline: .now() + firstTimeout + 0.1) { - result = limiter.canSubmit() + result = limiter.canSubmit(self.customBeacon) waitFor.fulfill() } wait(for: [waitFor], timeout: firstTimeout + 3) @@ -71,10 +102,10 @@ class ReporterRateLimitTests: InstanaTestCase { let limiter = ReporterRateLimiter(configs: configs) // When - var result = limiter.canSubmit() + var result = limiter.canSubmit(customBeacon) // Fire another one BEFORE the first limit has been cleared DispatchQueue.main.asyncAfter(deadline: .now() + firstTimeout - 0.1) { - result = limiter.canSubmit() + result = limiter.canSubmit(self.customBeacon) waitFor.fulfill() } wait(for: [waitFor], timeout: firstTimeout + 3) @@ -92,11 +123,11 @@ class ReporterRateLimitTests: InstanaTestCase { let limiter = ReporterRateLimiter(configs: configs) // When - var result = limiter.canSubmit() + var result = limiter.canSubmit(customBeacon) // Fire two AFTER the first limit has been cleared DispatchQueue.main.asyncAfter(deadline: .now() + firstTimeout + 0.1) { - result = limiter.canSubmit() - result = limiter.canSubmit() + result = limiter.canSubmit(self.customBeacon) + result = limiter.canSubmit(self.customBeacon) waitFor.fulfill() } wait(for: [waitFor], timeout: firstTimeout + 3) diff --git a/Tests/InstanaAgentTests/Configuration/InstanaSystemUtilsTests.swift b/Tests/InstanaAgentTests/Configuration/InstanaSystemUtilsTests.swift index 46164264..9ddc30cd 100644 --- a/Tests/InstanaAgentTests/Configuration/InstanaSystemUtilsTests.swift +++ b/Tests/InstanaAgentTests/Configuration/InstanaSystemUtilsTests.swift @@ -5,7 +5,7 @@ class InstanaSystemUtilsTests: InstanaTestCase { func test_AgentVersion() { // Then - AssertTrue(InstanaSystemUtils.agentVersion == "1.8.2") + AssertTrue(InstanaSystemUtils.agentVersion == "1.8.3") } func test_systemVersion() {