Skip to content

Commit

Permalink
Do not rate limit session start beacon and crash beacons
Browse files Browse the repository at this point in the history
  • Loading branch information
Hongyan Jiang authored and GitHub Enterprise committed Aug 22, 2024
1 parent 596e272 commit d71cffe
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 18 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion InstanaAgent.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Sources/InstanaAgent/Beacons/Reporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions Sources/InstanaAgent/Beacons/ReporterRateLimiter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion Sources/InstanaAgent/Configuration/Defines.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Sources/InstanaAgent/Configuration/VersionConfig.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
struct VersionConfig {
static let agentVersion = "1.8.2"
static let agentVersion = "1.8.3"
}
53 changes: 42 additions & 11 deletions Tests/InstanaAgentTests/Beacons/ReporterRateLimitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,38 @@ 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
let configs: [InstanaConfiguration.ReporterRateLimitConfig] = [.init(timeout: 0.5, maxItems: 1)]
let limiter = ReporterRateLimiter(configs: configs)

// When
let result = limiter.canSubmit()
let result = limiter.canSubmit(customBeacon)

// Then
XCTAssertTrue(result)
Expand All @@ -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() {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit d71cffe

Please sign in to comment.