diff --git a/README.md b/README.md index 95fd4ccd..84bb2204 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,7 @@ The configuration follows this JSON schema: | appMonitorId | String | Yes | Unique identifier for the RUM App Monitor | | overrideEndpoint | Object | No | Optional endpoint overrides for the RUM service | | debug | Boolean | No | Flag to enable debug logging (defaults to false) | +| alias | String | No | Adds an alias to all requests. It will be compared to the rum:alias service context key in the resource based policy attached to a RUM app monitor | #### ApplicationConfig diff --git a/Sources/AwsOpenTelemetryCore/AwsOpenTelemetryRumBuilder.swift b/Sources/AwsOpenTelemetryCore/AwsOpenTelemetryRumBuilder.swift index bb3ea086..1289fb50 100644 --- a/Sources/AwsOpenTelemetryCore/AwsOpenTelemetryRumBuilder.swift +++ b/Sources/AwsOpenTelemetryCore/AwsOpenTelemetryRumBuilder.swift @@ -212,11 +212,15 @@ public class AwsOpenTelemetryRumBuilder { * @return A resource with AWS RUM attributes */ private static func buildResource(config: AwsOpenTelemetryConfig) -> Resource { - let rumResourceAttributes: [String: String] = [ + var rumResourceAttributes: [String: String] = [ AwsRumConstants.AWS_REGION: config.rum.region, AwsRumConstants.RUM_APP_MONITOR_ID: config.rum.appMonitorId ] + if config.rum.alias?.isEmpty == false { + rumResourceAttributes[AwsRumConstants.RUM_ALIAS] = config.rum.alias! + } + let resource = DefaultResources().get() .merging(other: Resource(attributes: buildAttributeMap(rumResourceAttributes))) diff --git a/Sources/AwsOpenTelemetryCore/AwsRumConstants.swift b/Sources/AwsOpenTelemetryCore/AwsRumConstants.swift index 0d33edf7..b6a4602e 100644 --- a/Sources/AwsOpenTelemetryCore/AwsRumConstants.swift +++ b/Sources/AwsOpenTelemetryCore/AwsRumConstants.swift @@ -27,4 +27,7 @@ public enum AwsRumConstants { /// Attribute key for the RUM App Monitor ID public static let RUM_APP_MONITOR_ID = "awsRumAppMonitorId" + + /// Attribute key for the RUM alias + public static let RUM_ALIAS = "awsRumAlias" } diff --git a/Sources/AwsOpenTelemetryCore/Configuration/AwsOpenTelemetryConfig.swift b/Sources/AwsOpenTelemetryCore/Configuration/AwsOpenTelemetryConfig.swift index 83b5ea4c..48fe432d 100644 --- a/Sources/AwsOpenTelemetryCore/Configuration/AwsOpenTelemetryConfig.swift +++ b/Sources/AwsOpenTelemetryCore/Configuration/AwsOpenTelemetryConfig.swift @@ -67,6 +67,10 @@ import Foundation /// Flag to enable debug logging for SDK integration public var debug: Bool? + /// Optional alias to add to all requests to compare against the rum:alias + /// in appmonitors with resource based policies + public var alias: String? + /** * Initializes a new RUM configuration instance. * @@ -75,11 +79,12 @@ import Foundation * @param overrideEndpoint Optional endpoint overrides for the RUM service * @param debug Flag to enable debug logging (defaults to false) */ - @objc public init(region: String, appMonitorId: String, overrideEndpoint: EndpointOverrides? = nil, debug: Bool = false) { + @objc public init(region: String, appMonitorId: String, overrideEndpoint: EndpointOverrides? = nil, debug: Bool = false, alias: String? = nil) { self.region = region self.appMonitorId = appMonitorId self.overrideEndpoint = overrideEndpoint self.debug = debug + self.alias = alias super.init() } } diff --git a/Tests/AwsOpenTelemetryTests/Configuration/AwsOpenTelemetryConfigTests.swift b/Tests/AwsOpenTelemetryTests/Configuration/AwsOpenTelemetryConfigTests.swift index b0e5da5d..8a1b0e89 100644 --- a/Tests/AwsOpenTelemetryTests/Configuration/AwsOpenTelemetryConfigTests.swift +++ b/Tests/AwsOpenTelemetryTests/Configuration/AwsOpenTelemetryConfigTests.swift @@ -12,6 +12,7 @@ final class AwsOpenTelemetryConfigTests: XCTestCase { let tracesOnlyEndpoint = "https://traces-only.example.com" let debug = true let defaultVersion = "1.0.0" + let alias = "test-alias" func testConfigInitialization() { // Test basic initialization @@ -25,6 +26,7 @@ final class AwsOpenTelemetryConfigTests: XCTestCase { XCTAssertEqual(config.rum.region, region) XCTAssertEqual(config.rum.appMonitorId, appMonitorId) XCTAssertNil(config.rum.overrideEndpoint) + XCTAssertNil(config.rum.alias) XCTAssertEqual(config.rum.debug, false) XCTAssertEqual(config.application.applicationVersion, appVersion) } @@ -42,7 +44,8 @@ final class AwsOpenTelemetryConfigTests: XCTestCase { region: region, appMonitorId: appMonitorId, overrideEndpoint: endpointOverrides, - debug: debug + debug: debug, + alias: alias ), application: ApplicationConfig(applicationVersion: appVersion) ) @@ -51,8 +54,10 @@ final class AwsOpenTelemetryConfigTests: XCTestCase { XCTAssertEqual(config.rum.region, region) XCTAssertEqual(config.rum.appMonitorId, appMonitorId) XCTAssertNotNil(config.rum.overrideEndpoint) + XCTAssertNotNil(config.rum.alias) XCTAssertEqual(config.rum.overrideEndpoint?.logs, logsEndpoint) XCTAssertEqual(config.rum.overrideEndpoint?.traces, tracesEndpoint) + XCTAssertEqual(config.rum.alias, alias) XCTAssertEqual(config.rum.debug, debug) XCTAssertEqual(config.application.applicationVersion, appVersion) } diff --git a/Tests/AwsOpenTelemetryTests/Configuration/AwsRumConfigReaderTests.swift b/Tests/AwsOpenTelemetryTests/Configuration/AwsRumConfigReaderTests.swift index 4d130680..0eba376f 100644 --- a/Tests/AwsOpenTelemetryTests/Configuration/AwsRumConfigReaderTests.swift +++ b/Tests/AwsOpenTelemetryTests/Configuration/AwsRumConfigReaderTests.swift @@ -9,6 +9,7 @@ final class AwsRumConfigReaderTests: XCTestCase { let appVersion = "1.2.3" let logsEndpoint = "https://logs.example.com" let tracesEndpoint = "https://traces.example.com" + let alias = "test-alias" func testParseValidConfig() throws { // Create a valid JSON configuration @@ -68,6 +69,31 @@ final class AwsRumConfigReaderTests: XCTestCase { XCTAssertEqual(config.rum.overrideEndpoint?.traces, tracesEndpoint) } + func testParseConfigWithAlias() throws { + // Create a valid JSON configuration + let jsonString = """ + { + "version": "\(version)", + "rum": { + "region": "\(region)", + "appMonitorId": "\(appMonitorId)", + "alias": "\(alias)" + }, + "application": { + "applicationVersion": "\(appVersion)" + } + } + """ + + let data = jsonString.data(using: .utf8)! + + // Parse the configuration + let config = try AwsRumConfigReader.parseConfig(from: data) + + // Verify the alias + XCTAssertEqual(config.rum.alias, alias) + } + func testParseInvalidJson() { // Define test value let invalidJson = "{ this is not valid JSON }"