Skip to content

Commit 94b49e1

Browse files
Hongyan JiangGitHub Enterprise
authored andcommitted
usi fix & app foreground/background identification implementation (#84)
1 parent 5f3c8a2 commit 94b49e1

File tree

13 files changed

+122
-16
lines changed

13 files changed

+122
-16
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changelog
22

33
## 1.9.0
4+
- fix usi (user session id) bug for default configuration
5+
- send app foreground/background state along with each beacon
46
- add `trustDeviceTiming` configuration option for backend to trust the device timing
57
- enable app launch time monitoring by default, other performance monitoring off
68

Sources/InstanaAgent/Beacons/CoreBeacon/CoreBeacon.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,17 @@ struct CoreBeacon: Codable {
468468
*/
469469
var tdt: String?
470470

471+
/**
472+
* Current App State
473+
*
474+
* Beacon is generated during app foreground or background state
475+
*
476+
* Example: "f" - foreground
477+
* "b" - background
478+
* "u" - other states
479+
*/
480+
var cas: String?
481+
471482
/**
472483
* Crash Timestamp
473484
*

Sources/InstanaAgent/Beacons/CoreBeacon/CoreBeaconFactory.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ extension CoreBeacon {
215215
ui = beacon.crashSession.userID
216216
un = beacon.crashSession.userName
217217
ue = beacon.crashSession.userEmail
218+
cas = nil
218219

219220
m = MetaData()
220221
m![crashMetaKeyIsSymbolicated] = String(beacon.isSymbolicated)
@@ -310,6 +311,7 @@ extension CoreBeacon {
310311
cn: connection.cellular.carrierName,
311312
ct: connection.description,
312313
ect: ect?.description ?? connection.cellular.description,
313-
tdt: trustDeviceTiming.map { $0 ? "1" : nil } ?? nil)
314+
tdt: trustDeviceTiming.map { $0 ? "1" : nil } ?? nil,
315+
cas: InstanaApplicationStateHandler.shared.getAppStateForBeacon())
314316
}
315317
}

Sources/InstanaAgent/Configuration/InstanaSession.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class InstanaSession {
108108
usiActive = UUID(uuidString: idStr!)
109109
if usiActive == nil {
110110
UserDefaults.standard.removeObject(forKey: userSessionIDKey)
111-
} else {
111+
} else if config.usiRefreshTimeIntervalInHrs > usiTrackingNotAllowed {
112112
let startTimeRead = UserDefaults.standard.double(forKey: usi_startTimeKey)
113113
let now = Date().timeIntervalSince1970
114114
if startTimeRead > 0, startTimeRead <= now {
@@ -122,7 +122,7 @@ class InstanaSession {
122122
if usiActive == nil {
123123
return usiNew(config)
124124
}
125-
return (usiActive!, startTime!)
125+
return (usiActive!, startTime)
126126
}
127127

128128
private static func usiNew(_ config: InstanaConfiguration) -> (UUID?, TimeInterval?) {

Sources/InstanaAgent/Utils/InstanaApplicationStateHandler.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,15 @@ class InstanaApplicationStateHandler {
6969
func removeAllListener() {
7070
stateUpdateHandler.removeAll()
7171
}
72+
73+
func getAppStateForBeacon() -> String? {
74+
switch state {
75+
case .active:
76+
return "f"
77+
case .background:
78+
return "b"
79+
default:
80+
return "u"
81+
}
82+
}
7283
}

Tests/InstanaAgentTests/Beacons/Beacon Types/CustomBeaconTests.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class CustomBeaconTests: InstanaTestCase {
114114
let expectedCPU = InstanaSystemUtils.deviceModel
115115
let expectedErrorType = "\(type(of: customBeacon.error!))"
116116
let expectedErrorMessage = "\(customBeacon.error!)"
117-
let expected = "ab\t\(beacon.ab)\nagv\t\(beacon.agv)\nav\t\(InstanaSystemUtils.applicationVersion)\nbi\t\(beacon.bi)\nbid\t\(beacon.bid)\nbt\t\(customBeacon.backendTracingID ?? "")\ncen\t\(customBeacon.name)\ncm\t12.34567\ncn\tNone\nct\t\(InstanaSystemUtils.networkUtility.connectionType.description)\nd\t\(customBeacon.duration ?? 0)\ndma\tApple\ndmo\t\(expectedCPU)\nec\t1\nem\t\(expectedErrorMessage)\net\t\(expectedErrorType)\nk\tKEY\nm_\(customBeacon.metaData?.keys.first ?? "")\t\(customBeacon.metaData?.values.first ?? "")\nosn\tiOS\nosv\t\(InstanaSystemUtils.systemVersion)\np\tiOS\nro\t\(String(InstanaSystemUtils.isDeviceJailbroken))\nsid\t\(beacon.sid)\nt\tcustom\nti\t\(customBeacon.timestamp)\nuf\tc,lm\nul\ten\nusi\t\(mockedInstanaSession.usi!.uuidString)\nv\t\(customBeacon.viewName ?? "")\nvh\t\(Int(UIScreen.main.bounds.height))\nvw\t\(Int(UIScreen.main.bounds.width))"
117+
let expected = "ab\t\(beacon.ab)\nagv\t\(beacon.agv)\nav\t\(InstanaSystemUtils.applicationVersion)\nbi\t\(beacon.bi)\nbid\t\(beacon.bid)\nbt\t\(customBeacon.backendTracingID ?? "")\ncas\tf\ncen\t\(customBeacon.name)\ncm\t12.34567\ncn\tNone\nct\t\(InstanaSystemUtils.networkUtility.connectionType.description)\nd\t\(customBeacon.duration ?? 0)\ndma\tApple\ndmo\t\(expectedCPU)\nec\t1\nem\t\(expectedErrorMessage)\net\t\(expectedErrorType)\nk\tKEY\nm_\(customBeacon.metaData?.keys.first ?? "")\t\(customBeacon.metaData?.values.first ?? "")\nosn\tiOS\nosv\t\(InstanaSystemUtils.systemVersion)\np\tiOS\nro\t\(String(InstanaSystemUtils.isDeviceJailbroken))\nsid\t\(beacon.sid)\nt\tcustom\nti\t\(customBeacon.timestamp)\nuf\tc,lm\nul\ten\nusi\t\(mockedInstanaSession.usi!.uuidString)\nv\t\(customBeacon.viewName ?? "")\nvh\t\(Int(UIScreen.main.bounds.height))\nvw\t\(Int(UIScreen.main.bounds.width))"
118118
AssertEqualAndNotNil(sut, expected)
119119
}
120120

@@ -141,7 +141,7 @@ class CustomBeaconTests: InstanaTestCase {
141141
AssertEqualAndNotNil(beacon.v, nil)
142142

143143
let expectedCPU = InstanaSystemUtils.deviceModel
144-
let expected = "ab\t\(beacon.ab)\nagv\t\(beacon.agv)\nav\t\(InstanaSystemUtils.applicationVersion)\nbi\t\(beacon.bi)\nbid\t\(beacon.bid)\ncen\t\(name)\ncn\tNone\nct\t\(InstanaSystemUtils.networkUtility.connectionType.description)\ndma\tApple\ndmo\t\(expectedCPU)\nk\tKEY\nosn\tiOS\nosv\t\(InstanaSystemUtils.systemVersion)\np\tiOS\nro\t\(String(InstanaSystemUtils.isDeviceJailbroken))\nsid\t\(beacon.sid)\nt\tcustom\nti\t\(customBeacon.timestamp)\nuf\tc,lm\nul\ten\nusi\t\(session.usi!.uuidString)\nvh\t\(Int(UIScreen.main.bounds.height))\nvw\t\(Int(UIScreen.main.bounds.width))"
144+
let expected = "ab\t\(beacon.ab)\nagv\t\(beacon.agv)\nav\t\(InstanaSystemUtils.applicationVersion)\nbi\t\(beacon.bi)\nbid\t\(beacon.bid)\ncas\tf\ncen\t\(name)\ncn\tNone\nct\t\(InstanaSystemUtils.networkUtility.connectionType.description)\ndma\tApple\ndmo\t\(expectedCPU)\nk\tKEY\nosn\tiOS\nosv\t\(InstanaSystemUtils.systemVersion)\np\tiOS\nro\t\(String(InstanaSystemUtils.isDeviceJailbroken))\nsid\t\(beacon.sid)\nt\tcustom\nti\t\(customBeacon.timestamp)\nuf\tc,lm\nul\ten\nusi\t\(session.usi!.uuidString)\nvh\t\(Int(UIScreen.main.bounds.height))\nvw\t\(Int(UIScreen.main.bounds.width))"
145145
AssertEqualAndNotNil(sut, expected)
146146
}
147147

@@ -169,7 +169,9 @@ class CustomBeaconTests: InstanaTestCase {
169169
AssertEqualAndNotNil(customBeacon.viewName, CustomBeaconDefaultViewNameID)
170170
AssertEqualAndNotNil(beacon.v, viewName)
171171

172-
let expected = "ab\t\(beacon.ab)\nagv\t\(beacon.agv)\nav\t\(InstanaSystemUtils.applicationVersion)\nbi\t\(beacon.bi)\nbid\t\(beacon.bid)\ncen\t\(name)\ncn\tNone\nct\t\(InstanaSystemUtils.networkUtility.connectionType.description)\ndma\tApple\ndmo\t\(InstanaSystemUtils.deviceModel)\nk\tKEY\nosn\tiOS\nosv\t\(InstanaSystemUtils.systemVersion)\np\tiOS\nro\t\(String(InstanaSystemUtils.isDeviceJailbroken))\nsid\t\(beacon.sid)\nt\tcustom\nti\t\(customBeacon.timestamp)\nuf\tc,lm\nul\ten\nusi\t\(session.usi!.uuidString)\nv\t\(viewName)\nvh\t\(Int(UIScreen.main.bounds.height))\nvw\t\(Int(UIScreen.main.bounds.width))"
172+
let expected = "ab\t\(beacon.ab)\nagv\t\(beacon.agv)\nav\t\(InstanaSystemUtils.applicationVersion)\nbi\t\(beacon.bi)\nbid\t\(beacon.bid)\ncas\tf\ncen\t\(name)\ncn\tNone\nct\t\(InstanaSystemUtils.networkUtility.connectionType.description)\ndma\tApple\ndmo\t\(InstanaSystemUtils.deviceModel)\nk\tKEY\nosn\tiOS\nosv\t\(InstanaSystemUtils.systemVersion)\np\tiOS\nro\t\(String(InstanaSystemUtils.isDeviceJailbroken))\nsid\t\(beacon.sid)\nt\tcustom\nti\t\(customBeacon.timestamp)\nuf\tc,lm\nul\ten\nusi\t\(session.usi!.uuidString)\nv\t\(viewName)\nvh\t\(Int(UIScreen.main.bounds.height))\nvw\t\(Int(UIScreen.main.bounds.width))"
173+
print("hjtest exp \(expected)")
174+
print("hjtest sut \(sut!)")
173175
AssertEqualAndNotNil(sut, expected)
174176
}
175177

Tests/InstanaAgentTests/Beacons/Beacon Types/DiagnosticBeaconTest.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class DiagnosticBeaconTests: InstanaTestCase {
5454
AssertEqualAndNotNil(beacon.un, crashBeacon.crashSession.userName)
5555
AssertEqualAndNotNil(beacon.ue, crashBeacon.crashSession.userEmail)
5656

57+
XCTAssertNil(beacon.cas)
58+
5759
XCTAssertEqual(beacon.m![crashMetaKeyIsSymbolicated], String(crashBeacon.isSymbolicated))
5860
XCTAssertEqual(beacon.m![crashMetaKeyInstanaPayloadVersion], currentInstanaCrashPayloadVersion)
5961
XCTAssertEqual(beacon.m![crashMetaKeyCrashType], "crash")

Tests/InstanaAgentTests/Beacons/Beacon Types/HTTPBeaconTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class HTTPBeaconTests: InstanaTestCase {
146146

147147
// When
148148
let expectedErrorMessage = "\(HTTPError.timeout.rawValue): \(HTTPError.timeout.errorDescription)"
149-
let expected = "ab\t\(beacon.ab)\nagv\t\(beacon.agv)\nav\t\(beacon.av)\nbi\t\(beacon.bi)\nbid\t\(beacon.bid)\nbt\t\(backendTracingID)\ncn\t\(beacon.cn ?? "")\nct\t\(beacon.ct ?? "")\nd\t\(duration)\ndbs\t\(responseSize.bodyBytesAfterDecoding!)\ndma\tApple\ndmo\t\(beacon.dmo)\nebs\t\(responseSize.bodyBytes!)\nec\t1\nem\t\(expectedErrorMessage)\net\tHTTPError\nh_X-Key1\tSec\nh_X-Key2\tP\nhm\t\(method)\nhp\t\(url.path)\nhs\t\(responseCode)\nhu\t\(url.absoluteString)\nk\t\(key)\nosn\tiOS\nosv\t\(beacon.osv)\np\tiOS\nro\t\(String(InstanaSystemUtils.isDeviceJailbroken))\nsid\t\(beacon.sid)\nt\thttpRequest\nti\t\(timestamp)\ntrs\t\(responseSize.headerBytes! + responseSize.bodyBytes!)\nuf\tc,lm\nul\ten\nusi\t\(mockedInstanaSession.usi!.uuidString)\nv\t\(viewName)\nvh\t\(Int(UIScreen.main.bounds.height))\nvw\t\(Int(UIScreen.main.bounds.width))"
149+
let expected = "ab\t\(beacon.ab)\nagv\t\(beacon.agv)\nav\t\(beacon.av)\nbi\t\(beacon.bi)\nbid\t\(beacon.bid)\nbt\t\(backendTracingID)\ncas\tf\ncn\t\(beacon.cn ?? "")\nct\t\(beacon.ct ?? "")\nd\t\(duration)\ndbs\t\(responseSize.bodyBytesAfterDecoding!)\ndma\tApple\ndmo\t\(beacon.dmo)\nebs\t\(responseSize.bodyBytes!)\nec\t1\nem\t\(expectedErrorMessage)\net\tHTTPError\nh_X-Key1\tSec\nh_X-Key2\tP\nhm\t\(method)\nhp\t\(url.path)\nhs\t\(responseCode)\nhu\t\(url.absoluteString)\nk\t\(key)\nosn\tiOS\nosv\t\(beacon.osv)\np\tiOS\nro\t\(String(InstanaSystemUtils.isDeviceJailbroken))\nsid\t\(beacon.sid)\nt\thttpRequest\nti\t\(timestamp)\ntrs\t\(responseSize.headerBytes! + responseSize.bodyBytes!)\nuf\tc,lm\nul\ten\nusi\t\(mockedInstanaSession.usi!.uuidString)\nv\t\(viewName)\nvh\t\(Int(UIScreen.main.bounds.height))\nvw\t\(Int(UIScreen.main.bounds.width))"
150150
XCTAssertEqual(sut, expected)
151151
}
152152

Tests/InstanaAgentTests/Beacons/Beacon Types/PerfLowMemoryBeaconTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class PerfLowMemoryBeaconTests: InstanaTestCase {
2121
AssertTrue(sut.em == nil)
2222

2323
let values = Mirror(reflecting: sut).nonNilChildren
24-
XCTAssertEqual(values.count, 26)
24+
XCTAssertEqual(values.count, 27)
2525
}
2626

2727
func test_asString() {
@@ -39,7 +39,7 @@ class PerfLowMemoryBeaconTests: InstanaTestCase {
3939
let sut = beacon.asString
4040

4141
// When
42-
let expected = "ab\t\(beacon.ab)\nagv\t\(beacon.agv)\namb\t60\nav\t\(beacon.av)\nbi\t\(beacon.bi)\nbid\t\(beacon.bid)\ncn\t\(beacon.cn ?? "")\nct\t\(beacon.ct ?? "")\ndma\tApple\ndmo\t\(beacon.dmo)\nk\t\(key)\nmmb\t32686\nosn\tiOS\nosv\t\(beacon.osv)\np\tiOS\npst\toom\nro\t\(String(InstanaSystemUtils.isDeviceJailbroken))\nsid\t\(beacon.sid)\nt\tperf\nti\t\(perf.timestamp)\nuf\tc,lm\nul\ten\numb\t20\nusi\t\(mockedInstanaSession.usi!.uuidString)\nvh\t\(Int(UIScreen.main.bounds.height))\nvw\t\(Int(UIScreen.main.bounds.width))"
42+
let expected = "ab\t\(beacon.ab)\nagv\t\(beacon.agv)\namb\t60\nav\t\(beacon.av)\nbi\t\(beacon.bi)\nbid\t\(beacon.bid)\ncas\tf\ncn\t\(beacon.cn ?? "")\nct\t\(beacon.ct ?? "")\ndma\tApple\ndmo\t\(beacon.dmo)\nk\t\(key)\nmmb\t32686\nosn\tiOS\nosv\t\(beacon.osv)\np\tiOS\npst\toom\nro\t\(String(InstanaSystemUtils.isDeviceJailbroken))\nsid\t\(beacon.sid)\nt\tperf\nti\t\(perf.timestamp)\nuf\tc,lm\nul\ten\numb\t20\nusi\t\(mockedInstanaSession.usi!.uuidString)\nvh\t\(Int(UIScreen.main.bounds.height))\nvw\t\(Int(UIScreen.main.bounds.width))"
4343
XCTAssertEqual(sut, expected)
4444
}
4545

Tests/InstanaAgentTests/Beacons/Beacon Types/SessionProfileBeaconTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class SessionProfileBeaconTests: InstanaTestCase {
3636
let sut = beacon.asString
3737

3838
// When
39-
let expected = "ab\t\(beacon.ab)\nagv\t\(beacon.agv)\nav\t\(beacon.av)\nbi\t\(beacon.bi)\nbid\t\(beacon.bid)\ncn\t\(beacon.cn ?? "")\nct\t\(beacon.ct ?? "")\ndma\tApple\ndmo\t\(beacon.dmo)\nk\t\(key)\nosn\tiOS\nosv\t\(beacon.osv)\np\tiOS\nro\t\(String(InstanaSystemUtils.isDeviceJailbroken))\nsid\t\(beacon.sid)\nt\tsessionStart\nti\t\(session.timestamp)\nuf\tc,lm\nul\ten\nusi\t\(mockInstanaSession.usi!.uuidString)\nvh\t\(Int(UIScreen.main.bounds.height))\nvw\t\(Int(UIScreen.main.bounds.width))"
39+
let expected = "ab\t\(beacon.ab)\nagv\t\(beacon.agv)\nav\t\(beacon.av)\nbi\t\(beacon.bi)\nbid\t\(beacon.bid)\ncas\tf\ncn\t\(beacon.cn ?? "")\nct\t\(beacon.ct ?? "")\ndma\tApple\ndmo\t\(beacon.dmo)\nk\t\(key)\nosn\tiOS\nosv\t\(beacon.osv)\np\tiOS\nro\t\(String(InstanaSystemUtils.isDeviceJailbroken))\nsid\t\(beacon.sid)\nt\tsessionStart\nti\t\(session.timestamp)\nuf\tc,lm\nul\ten\nusi\t\(mockInstanaSession.usi!.uuidString)\nvh\t\(Int(UIScreen.main.bounds.height))\nvw\t\(Int(UIScreen.main.bounds.width))"
4040
XCTAssertEqual(sut, expected)
4141
}
4242

0 commit comments

Comments
 (0)