From 6d9cc37c1e5437f585e55e779c38ff9eb129bcc4 Mon Sep 17 00:00:00 2001 From: Hongyan Jiang Date: Mon, 19 Feb 2024 10:10:18 -0800 Subject: [PATCH] Add optional CustomMetric field to Custom Beacon --- Changelog.md | 3 +++ Dev/ObjectiveCAppExample/AppDelegate.m | 9 +++++++++ InstanaAgent.podspec | 2 +- .../Beacons/Beacons Types/CustomBeacon.swift | 5 ++++- .../Beacons/CoreBeacon/CoreBeacon.swift | 7 +++++++ .../Beacons/CoreBeacon/CoreBeaconFactory.swift | 3 +++ .../Configuration/VersionConfig.swift | 2 +- Sources/InstanaAgent/Instana.swift | 15 +++++++++++++-- .../Beacons/Beacon Types/CustomBeaconTests.swift | 7 +++++-- .../Beacons/CoreBeacon/CoreBeaconTests.swift | 4 ++-- .../Configuration/InstanaSystemUtilsTests.swift | 2 +- 11 files changed, 49 insertions(+), 10 deletions(-) diff --git a/Changelog.md b/Changelog.md index b98ec85..5bee0b1 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,8 @@ # Changelog +## 1.6.9 +- Support CustomMetric in reportEvent method + ## 1.6.8 - Add more meta data to crash beacon - Add more exceptionType processing to crash beacon diff --git a/Dev/ObjectiveCAppExample/AppDelegate.m b/Dev/ObjectiveCAppExample/AppDelegate.m index c5aec13..022d23e 100644 --- a/Dev/ObjectiveCAppExample/AppDelegate.m +++ b/Dev/ObjectiveCAppExample/AppDelegate.m @@ -37,6 +37,15 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( NSURLRequest* request = [NSURLRequest requestWithURL: url]; [[[NSURLSession sharedSession] dataTaskWithRequest: request] resume]; + [Instana reportEventWithName: @"testCustomEventName" + timestamp: NSNotFound + duration: NSNotFound + backendTracingID: nil + error: nil + meta: nil + viewName: nil + customMetric: NAN]; //"0x7fc00000" + return YES; } diff --git a/InstanaAgent.podspec b/InstanaAgent.podspec index 54abbd5..2400abe 100644 --- a/InstanaAgent.podspec +++ b/InstanaAgent.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |s| # s.name = "InstanaAgent" - s.version = "1.6.8" + s.version = "1.6.9" s.summary = "Instana iOS agent." # This description is used to generate tags and improve search results. diff --git a/Sources/InstanaAgent/Beacons/Beacons Types/CustomBeacon.swift b/Sources/InstanaAgent/Beacons/Beacons Types/CustomBeacon.swift index b4b93a7..098e8d9 100644 --- a/Sources/InstanaAgent/Beacons/Beacons Types/CustomBeacon.swift +++ b/Sources/InstanaAgent/Beacons/Beacons Types/CustomBeacon.swift @@ -15,6 +15,7 @@ class CustomBeacon: Beacon { let backendTracingID: String? let error: Error? let metaData: MetaData? + let customMetric: Double? init(timestamp: Instana.Types.Milliseconds? = nil, name: String, @@ -22,7 +23,8 @@ class CustomBeacon: Beacon { backendTracingID: String? = nil, error: Error? = nil, metaData: MetaData? = nil, - viewName: String? = CustomBeaconDefaultViewNameID) { + viewName: String? = CustomBeaconDefaultViewNameID, + customMetric: Double? = nil) { self.duration = duration self.name = name self.error = error @@ -35,6 +37,7 @@ class CustomBeacon: Beacon { if let timestamp = timestamp { start = timestamp } + self.customMetric = customMetric super.init(timestamp: start, viewName: viewName) } } diff --git a/Sources/InstanaAgent/Beacons/CoreBeacon/CoreBeacon.swift b/Sources/InstanaAgent/Beacons/CoreBeacon/CoreBeacon.swift index 9189877..2618469 100644 --- a/Sources/InstanaAgent/Beacons/CoreBeacon/CoreBeacon.swift +++ b/Sources/InstanaAgent/Beacons/CoreBeacon/CoreBeacon.swift @@ -125,6 +125,13 @@ struct CoreBeacon: Codable { */ var cen: String? + /** + * CustomMetric + * + * For example: "123.4567" + */ + var cm: String? + /** * Meta for custom key/value entries * diff --git a/Sources/InstanaAgent/Beacons/CoreBeacon/CoreBeaconFactory.swift b/Sources/InstanaAgent/Beacons/CoreBeacon/CoreBeaconFactory.swift index 24121e4..6d7765f 100644 --- a/Sources/InstanaAgent/Beacons/CoreBeacon/CoreBeaconFactory.swift +++ b/Sources/InstanaAgent/Beacons/CoreBeacon/CoreBeaconFactory.swift @@ -167,6 +167,9 @@ extension CoreBeacon { if let tracingID = beacon.backendTracingID { bt = tracingID } + if let customMetric = beacon.customMetric { + cm = String(customMetric) + } } private mutating func add(error: Error) { diff --git a/Sources/InstanaAgent/Configuration/VersionConfig.swift b/Sources/InstanaAgent/Configuration/VersionConfig.swift index 7dbdc25..438400a 100644 --- a/Sources/InstanaAgent/Configuration/VersionConfig.swift +++ b/Sources/InstanaAgent/Configuration/VersionConfig.swift @@ -1,3 +1,3 @@ struct VersionConfig { - static let agentVersion = "1.6.8" + static let agentVersion = "1.6.9" } diff --git a/Sources/InstanaAgent/Instana.swift b/Sources/InstanaAgent/Instana.swift index e5eaf2d..e996a65 100644 --- a/Sources/InstanaAgent/Instana.swift +++ b/Sources/InstanaAgent/Instana.swift @@ -435,19 +435,30 @@ import Foundation /// Alternatively you can leave out the parameter or send /// nil to use the current view name implicitly you did set in `setView(name: String)` @objc - public static func reportEvent(name: String, timestamp: Instana.Types.Milliseconds = Instana.Types.Milliseconds(NSNotFound), duration: Instana.Types.Milliseconds = Instana.Types.Milliseconds(NSNotFound), backendTracingID: String? = nil, error: Error? = nil, meta: [String: String]? = nil, viewName: String? = nil) { + public static func reportEvent(name: String, + timestamp: Instana.Types.Milliseconds = Instana.Types.Milliseconds(NSNotFound), + duration: Instana.Types.Milliseconds = Instana.Types.Milliseconds(NSNotFound), + backendTracingID: String? = nil, + error: Error? = nil, + meta: [String: String]? = nil, + viewName: String? = nil, + customMetric: Double = Double.nan) { // As a workaround for primitive values in ObjC let timestamp = timestamp == NSNotFound ? nil : timestamp let duration = duration == NSNotFound ? nil : duration var viewName = viewName ?? CustomBeaconDefaultViewNameID viewName = !viewName.isEmpty ? viewName : CustomBeaconDefaultViewNameID + + let customMetric: Double? = customMetric.isNaN ? nil : customMetric + let beacon = CustomBeacon(timestamp: timestamp, name: name, duration: duration, backendTracingID: backendTracingID, error: error, metaData: meta, - viewName: viewName) + viewName: viewName, + customMetric: customMetric) Instana.current?.monitors.reporter.submit(beacon) } diff --git a/Tests/InstanaAgentTests/Beacons/Beacon Types/CustomBeaconTests.swift b/Tests/InstanaAgentTests/Beacons/Beacon Types/CustomBeaconTests.swift index 0d43610..2d01229 100644 --- a/Tests/InstanaAgentTests/Beacons/Beacon Types/CustomBeaconTests.swift +++ b/Tests/InstanaAgentTests/Beacons/Beacon Types/CustomBeaconTests.swift @@ -114,7 +114,7 @@ class CustomBeaconTests: InstanaTestCase { let expectedCPU = InstanaSystemUtils.deviceModel let expectedErrorType = "\(type(of: customBeacon.error!))" let expectedErrorMessage = "\(customBeacon.error!)" - 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)\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\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))" + 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\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))" AssertEqualAndNotNil(sut, expected) } @@ -212,6 +212,9 @@ class CustomBeaconTests: InstanaTestCase { let backendTracingID = "BID" let error = SomeBeaconError.something let metaData = ["Key": "SomeValue"] - return CustomBeacon(timestamp: timestamp, name: name, duration: duration, backendTracingID: backendTracingID, error: error, metaData: metaData, viewName: viewName) + let customMetric = 12.34567 + return CustomBeacon(timestamp: timestamp, name: name, duration: duration, + backendTracingID: backendTracingID, error: error, metaData: metaData, + viewName: viewName, customMetric: customMetric) } } diff --git a/Tests/InstanaAgentTests/Beacons/CoreBeacon/CoreBeaconTests.swift b/Tests/InstanaAgentTests/Beacons/CoreBeacon/CoreBeaconTests.swift index 3b1c0c8..fac7da9 100644 --- a/Tests/InstanaAgentTests/Beacons/CoreBeacon/CoreBeaconTests.swift +++ b/Tests/InstanaAgentTests/Beacons/CoreBeacon/CoreBeaconTests.swift @@ -97,7 +97,7 @@ class CoreBeaconTests: InstanaTestCase { let values = Mirror(reflecting: sut).children // Then - XCTAssertEqual(values.count, 47) + XCTAssertEqual(values.count, 48) } func testNumberOfFields_non_nil() { @@ -119,7 +119,7 @@ class CoreBeaconTests: InstanaTestCase { let expectedKeys = ["t", "v", "bt", "k" ,"ti", "sid", "usi", "bid", "uf", "bi", "m", "ui", "un", "ue", "ul", "ab", "av", "p", "osn", "osv", "dma", "dmo", "ro", "vw", "vh", "cn", "ct", "ect", "hu", "hp", "hm", "hs", "ebs", "dbs", "trs", "d", - "ec", "em", "et", "agv", "cen", "h", "ast", "cid", "cti", "dt", "st"] + "ec", "em", "et", "agv", "cen", "cm", "h", "ast", "cid", "cti", "dt", "st"] // When let keys = Mirror(reflecting: sut).children.compactMap {$0.label} diff --git a/Tests/InstanaAgentTests/Configuration/InstanaSystemUtilsTests.swift b/Tests/InstanaAgentTests/Configuration/InstanaSystemUtilsTests.swift index a9d4efd..a76a299 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.6.8") + AssertTrue(InstanaSystemUtils.agentVersion == "1.6.9") } func test_systemVersion() {