From 70a5d100be6c839931189cd59e2226ecbe3c951e Mon Sep 17 00:00:00 2001 From: Maciej Burda Date: Fri, 2 Dec 2022 16:40:51 +0000 Subject: [PATCH] RUMM-2759 Replace firstPartHosts --- .../Datadog/Core/FeaturesConfiguration.swift | 16 +++--- .../Datadog/Core/Utils/HostsSanitizer.swift | 10 ++-- Sources/Datadog/DatadogConfiguration.swift | 13 ++--- Sources/Datadog/Logger.swift | 1 - .../TracingHeaderType.swift | 8 +++ .../TracingHeaderTypesProvider.swift | 14 ++--- .../Interception/URLSessionInterceptor.swift | 4 +- .../Core/FeaturesConfigurationTests.swift | 51 +++++++++++-------- .../DatadogConfigurationBuilderTests.swift | 6 +-- .../Datadog/Mocks/CoreMocks.swift | 18 +++---- .../TracingHeaderTypesProviderTests.swift | 23 +++++---- .../FirstPartyURLsFilterTests.swift | 22 ++++++++ .../URLSessionInterceptorTests.swift | 1 - .../DatadogObjc/DDConfigurationTests.swift | 4 +- 14 files changed, 110 insertions(+), 81 deletions(-) diff --git a/Sources/Datadog/Core/FeaturesConfiguration.swift b/Sources/Datadog/Core/FeaturesConfiguration.swift index cad799c912..353ba32256 100644 --- a/Sources/Datadog/Core/FeaturesConfiguration.swift +++ b/Sources/Datadog/Core/FeaturesConfiguration.swift @@ -75,11 +75,8 @@ internal struct FeaturesConfiguration { } struct URLSessionAutoInstrumentation { - /// First party hosts defined by the user. - let userDefinedFirstPartyHosts: Set - /// First party hosts defined by the user with custom tracing header types. - let userDefinedHostsWithHeaderTypes: [String: Set] + let userDefinedHostsWithHeaderTypes: FirstPartyHosts /// URLs used internally by the SDK - they are not instrumented. let sdkInternalURLs: Set @@ -213,7 +210,7 @@ extension FeaturesConfiguration { } var sanitizedHosts: Set = [] - if let firstPartyHosts = configuration.firstPartyHosts { + if let firstPartyHosts = configuration.firstPartyHostsWithHeaderTypes?.hosts { sanitizedHosts = hostsSanitizer.sanitized( hosts: firstPartyHosts, warningMessage: "The first party host configured for Datadog SDK is not valid" @@ -258,18 +255,17 @@ extension FeaturesConfiguration { } } - var sanitizedHostsWithHeaderTypes: [String: Set] = [:] - if let hostsWithHeaderTypes = configuration.firstPartyHostsWithHeaderTypes { + var sanitizedHostsWithHeaderTypes: FirstPartyHosts = [:] + if let firstPartyHosts = configuration.firstPartyHostsWithHeaderTypes { sanitizedHostsWithHeaderTypes = hostsSanitizer.sanitized( - hostsWithHeaderTypes: hostsWithHeaderTypes, + firstPartyHosts: firstPartyHosts, warningMessage: "The first party host with header types configured for Datadog SDK is not valid" ) } - if configuration.firstPartyHosts != nil { + if configuration.firstPartyHostsWithHeaderTypes?.hosts != nil { if configuration.tracingEnabled || configuration.rumEnabled { urlSessionAutoInstrumentation = URLSessionAutoInstrumentation( - userDefinedFirstPartyHosts: sanitizedHosts, userDefinedHostsWithHeaderTypes: sanitizedHostsWithHeaderTypes, sdkInternalURLs: [ logsEndpoint.url, diff --git a/Sources/Datadog/Core/Utils/HostsSanitizer.swift b/Sources/Datadog/Core/Utils/HostsSanitizer.swift index baaeb98905..022a95eec4 100644 --- a/Sources/Datadog/Core/Utils/HostsSanitizer.swift +++ b/Sources/Datadog/Core/Utils/HostsSanitizer.swift @@ -9,9 +9,9 @@ import Foundation internal protocol HostsSanitizing { func sanitized(hosts: Set, warningMessage: String) -> Set func sanitized( - hostsWithHeaderTypes: [String: Set], + firstPartyHosts: FirstPartyHosts, warningMessage: String - ) -> [String: Set] + ) -> FirstPartyHosts } internal struct HostsSanitizer: HostsSanitizing { @@ -60,12 +60,12 @@ internal struct HostsSanitizer: HostsSanitizing { } func sanitized( - hostsWithHeaderTypes: [String: Set], + firstPartyHosts: FirstPartyHosts, warningMessage: String - ) -> [String: Set] { + ) -> FirstPartyHosts { var warnings: [String] = [] - let sanitized: [String: Set] = hostsWithHeaderTypes.reduce(into: [:]) { partialResult, item in + let sanitized: FirstPartyHosts = firstPartyHosts.reduce(into: [:]) { partialResult, item in let host = item.key if host.range(of: urlRegex, options: .regularExpression) != nil { // if an URL is given instead of the host, take its `host` part diff --git a/Sources/Datadog/DatadogConfiguration.swift b/Sources/Datadog/DatadogConfiguration.swift index 8a901f1651..18094a3012 100644 --- a/Sources/Datadog/DatadogConfiguration.swift +++ b/Sources/Datadog/DatadogConfiguration.swift @@ -262,8 +262,7 @@ extension Datadog { private(set) var rumEndpoint: RUMEndpoint private(set) var serviceName: String? - private(set) var firstPartyHosts: Set? - private(set) var firstPartyHostsWithHeaderTypes: [String: Set]? + private(set) var firstPartyHostsWithHeaderTypes: FirstPartyHosts? private(set) var logEventMapper: LogEventMapper? private(set) var spanEventMapper: SpanEventMapper? private(set) var loggingSamplingRate: Float @@ -343,7 +342,6 @@ extension Datadog { tracesEndpoint: .us1, rumEndpoint: .us1, serviceName: nil, - firstPartyHosts: nil, firstPartyHostsWithHeaderTypes: nil, spanEventMapper: nil, loggingSamplingRate: 100.0, @@ -530,16 +528,13 @@ extension Datadog { /// /// - Parameter firstPartyHosts: empty set by default public func trackURLSession(firstPartyHosts: Set = []) -> Builder { - configuration.firstPartyHosts = firstPartyHosts - configuration.firstPartyHostsWithHeaderTypes = firstPartyHosts.reduce(into: [:], { partialResult, host in + return trackURLSession(firstPartyHostsWithHeaderTypes: firstPartyHosts.reduce(into: [:], { partialResult, host in partialResult[host] = .init(arrayLiteral: .dd) - }) - return self + })) } - public func trackURLSession(firstPartyHostsWithHeaderTypes: [String: Set]) -> Builder { + public func trackURLSession(firstPartyHostsWithHeaderTypes: FirstPartyHosts) -> Builder { configuration.firstPartyHostsWithHeaderTypes = firstPartyHostsWithHeaderTypes - configuration.firstPartyHosts = Set(firstPartyHostsWithHeaderTypes.keys) return self } diff --git a/Sources/Datadog/Logger.swift b/Sources/Datadog/Logger.swift index 135f871cc2..adea91b40e 100644 --- a/Sources/Datadog/Logger.swift +++ b/Sources/Datadog/Logger.swift @@ -274,7 +274,6 @@ public class Logger: LoggerProtocol { internal var sendLogsToDatadog = true internal var consoleLogFormat: ConsoleLogFormat? = nil internal var datadogReportingThreshold: LogLevel = .debug - internal var samplingRate: Float? /// Sets the service name that will appear in logs. /// - Parameter serviceName: the service name (default value is set to application bundle identifier) diff --git a/Sources/Datadog/Tracing/AutoInstrumentation/TracingHeaderType.swift b/Sources/Datadog/Tracing/AutoInstrumentation/TracingHeaderType.swift index dd53e9e844..d663177305 100644 --- a/Sources/Datadog/Tracing/AutoInstrumentation/TracingHeaderType.swift +++ b/Sources/Datadog/Tracing/AutoInstrumentation/TracingHeaderType.swift @@ -18,3 +18,11 @@ public enum TracingHeaderType: Hashable { case b3m case w3c } + +public typealias FirstPartyHosts = [String: Set] + +extension FirstPartyHosts { + var hosts: Set { + return Set(keys) + } +} diff --git a/Sources/Datadog/URLSessionAutoInstrumentation/Interception/TracingHeaderTypesProvider/TracingHeaderTypesProvider.swift b/Sources/Datadog/URLSessionAutoInstrumentation/Interception/TracingHeaderTypesProvider/TracingHeaderTypesProvider.swift index bd7b978255..e2250c629f 100644 --- a/Sources/Datadog/URLSessionAutoInstrumentation/Interception/TracingHeaderTypesProvider/TracingHeaderTypesProvider.swift +++ b/Sources/Datadog/URLSessionAutoInstrumentation/Interception/TracingHeaderTypesProvider/TracingHeaderTypesProvider.swift @@ -7,22 +7,24 @@ import Foundation internal struct TracingHeaderTypesProvider { - private let hostsWithHeaderTypes: [String: Set] - private let defaultValue: TracingHeaderType = .dd + private let firstPartyHosts: FirstPartyHosts init( - hostsWithHeaderTypes: [String: Set] + firstPartyHosts: FirstPartyHosts ) { - self.hostsWithHeaderTypes = hostsWithHeaderTypes + self.firstPartyHosts = firstPartyHosts } func tracingHeaderTypes(for url: URL?) -> Set { - for (key, value) in hostsWithHeaderTypes { + for (key, value) in firstPartyHosts { let regex = "^(.*\\.)*[.]?\(NSRegularExpression.escapedPattern(for: key))$" + if url?.host?.range(of: regex, options: .regularExpression) != nil { + return value + } if url?.absoluteString.range(of: regex, options: .regularExpression) != nil { return value } } - return .init(arrayLiteral: defaultValue) + return .init() } } diff --git a/Sources/Datadog/URLSessionAutoInstrumentation/Interception/URLSessionInterceptor.swift b/Sources/Datadog/URLSessionAutoInstrumentation/Interception/URLSessionInterceptor.swift index e4d09ae30b..2ef82a0b5c 100644 --- a/Sources/Datadog/URLSessionAutoInstrumentation/Interception/URLSessionInterceptor.swift +++ b/Sources/Datadog/URLSessionAutoInstrumentation/Interception/URLSessionInterceptor.swift @@ -80,10 +80,10 @@ public class URLSessionInterceptor: URLSessionInterceptorType { configuration: FeaturesConfiguration.URLSessionAutoInstrumentation, handler: URLSessionInterceptionHandler ) { - self.defaultFirstPartyURLsFilter = FirstPartyURLsFilter(hosts: configuration.userDefinedFirstPartyHosts) + self.defaultFirstPartyURLsFilter = FirstPartyURLsFilter(hosts: configuration.userDefinedHostsWithHeaderTypes.hosts) self.internalURLsFilter = InternalURLsFilter(urls: configuration.sdkInternalURLs) self.tracingHeaderTypesProvider = TracingHeaderTypesProvider( - hostsWithHeaderTypes: configuration.userDefinedHostsWithHeaderTypes + firstPartyHosts: configuration.userDefinedHostsWithHeaderTypes ) self.handler = handler self.tracingSampler = configuration.tracingSampler diff --git a/Tests/DatadogTests/Datadog/Core/FeaturesConfigurationTests.swift b/Tests/DatadogTests/Datadog/Core/FeaturesConfigurationTests.swift index 011007be75..bdc88a0667 100644 --- a/Tests/DatadogTests/Datadog/Core/FeaturesConfigurationTests.swift +++ b/Tests/DatadogTests/Datadog/Core/FeaturesConfigurationTests.swift @@ -570,7 +570,10 @@ class FeaturesConfigurationTests: XCTestCase { let randomCustomTracesEndpoint: URL? = Bool.random() ? .mockRandom() : nil let randomCustomRUMEndpoint: URL? = Bool.random() ? .mockRandom() : nil - let firstPartyHosts: Set = ["example.com", "foo.eu"] + let firstPartyHostsWithHeaderTypes: FirstPartyHosts = [ + "example.com": .init(arrayLiteral: .dd), + "foo.eu": .init(arrayLiteral: .dd) + ] let expectedSDKInternalURLs: Set = [ randomCustomLogsEndpoint?.absoluteString ?? randomDatadogEndpoint.logsEndpoint.url, randomCustomTracesEndpoint?.absoluteString ?? randomDatadogEndpoint.tracesEndpoint.url, @@ -580,7 +583,7 @@ class FeaturesConfigurationTests: XCTestCase { func createConfiguration( tracingEnabled: Bool, rumEnabled: Bool, - firstPartyHosts: Set? + firstPartyHostsWithHeaderTypes: FirstPartyHosts? ) throws -> FeaturesConfiguration { try FeaturesConfiguration( configuration: .mockWith( @@ -590,7 +593,7 @@ class FeaturesConfigurationTests: XCTestCase { customLogsEndpoint: randomCustomLogsEndpoint, customTracesEndpoint: randomCustomTracesEndpoint, customRUMEndpoint: randomCustomRUMEndpoint, - firstPartyHosts: firstPartyHosts + firstPartyHostsWithHeaderTypes: firstPartyHostsWithHeaderTypes ), appContext: .mockAny() ) @@ -600,9 +603,9 @@ class FeaturesConfigurationTests: XCTestCase { var configuration = try createConfiguration( tracingEnabled: true, rumEnabled: true, - firstPartyHosts: firstPartyHosts + firstPartyHostsWithHeaderTypes: firstPartyHostsWithHeaderTypes ) - XCTAssertEqual(configuration.urlSessionAutoInstrumentation?.userDefinedFirstPartyHosts, firstPartyHosts) + XCTAssertEqual(configuration.urlSessionAutoInstrumentation?.userDefinedHostsWithHeaderTypes, firstPartyHostsWithHeaderTypes) XCTAssertEqual(configuration.urlSessionAutoInstrumentation?.sdkInternalURLs, expectedSDKInternalURLs) XCTAssertTrue(configuration.urlSessionAutoInstrumentation!.instrumentTracing) XCTAssertTrue(configuration.urlSessionAutoInstrumentation!.instrumentRUM) @@ -611,9 +614,9 @@ class FeaturesConfigurationTests: XCTestCase { configuration = try createConfiguration( tracingEnabled: true, rumEnabled: false, - firstPartyHosts: firstPartyHosts + firstPartyHostsWithHeaderTypes: firstPartyHostsWithHeaderTypes ) - XCTAssertEqual(configuration.urlSessionAutoInstrumentation?.userDefinedFirstPartyHosts, firstPartyHosts) + XCTAssertEqual(configuration.urlSessionAutoInstrumentation?.userDefinedHostsWithHeaderTypes, firstPartyHostsWithHeaderTypes) XCTAssertEqual(configuration.urlSessionAutoInstrumentation?.sdkInternalURLs, expectedSDKInternalURLs) XCTAssertTrue(configuration.urlSessionAutoInstrumentation!.instrumentTracing) XCTAssertFalse(configuration.urlSessionAutoInstrumentation!.instrumentRUM) @@ -622,9 +625,9 @@ class FeaturesConfigurationTests: XCTestCase { configuration = try createConfiguration( tracingEnabled: false, rumEnabled: true, - firstPartyHosts: firstPartyHosts + firstPartyHostsWithHeaderTypes: firstPartyHostsWithHeaderTypes ) - XCTAssertEqual(configuration.urlSessionAutoInstrumentation?.userDefinedFirstPartyHosts, firstPartyHosts) + XCTAssertEqual(configuration.urlSessionAutoInstrumentation?.userDefinedHostsWithHeaderTypes, firstPartyHostsWithHeaderTypes) XCTAssertEqual(configuration.urlSessionAutoInstrumentation?.sdkInternalURLs, expectedSDKInternalURLs) XCTAssertFalse(configuration.urlSessionAutoInstrumentation!.instrumentTracing) XCTAssertTrue(configuration.urlSessionAutoInstrumentation!.instrumentRUM) @@ -633,7 +636,7 @@ class FeaturesConfigurationTests: XCTestCase { configuration = try createConfiguration( tracingEnabled: true, rumEnabled: true, - firstPartyHosts: nil + firstPartyHostsWithHeaderTypes: nil ) XCTAssertNil( configuration.urlSessionAutoInstrumentation, @@ -644,7 +647,7 @@ class FeaturesConfigurationTests: XCTestCase { configuration = try createConfiguration( tracingEnabled: true, rumEnabled: true, - firstPartyHosts: [] + firstPartyHostsWithHeaderTypes: [:] ) XCTAssertNotNil( configuration.urlSessionAutoInstrumentation, @@ -658,7 +661,7 @@ class FeaturesConfigurationTests: XCTestCase { configuration: .mockWith( tracingEnabled: .random(), rumEnabled: true, - firstPartyHosts: ["foo.com"], + firstPartyHostsWithHeaderTypes: ["foo.com": .init(arrayLiteral: .dd)], rumResourceAttributesProvider: { _, _, _, _ in [:] } ), appContext: .mockAny() @@ -667,7 +670,7 @@ class FeaturesConfigurationTests: XCTestCase { configuration: .mockWith( tracingEnabled: .random(), rumEnabled: true, - firstPartyHosts: ["foo.com"], + firstPartyHostsWithHeaderTypes: ["foo.com": .init(arrayLiteral: .dd)], rumResourceAttributesProvider: nil ), appContext: .mockAny() @@ -685,7 +688,7 @@ class FeaturesConfigurationTests: XCTestCase { _ = try FeaturesConfiguration( configuration: .mockWith( - firstPartyHosts: nil, + firstPartyHostsWithHeaderTypes: nil, rumResourceAttributesProvider: { _, _, _, _ in nil } ), appContext: .mockAny() @@ -742,7 +745,7 @@ class FeaturesConfigurationTests: XCTestCase { defer { consolePrint = { print($0) } } // Given - let firstPartyHosts: Set = ["first-party.com"] + let firstPartyHostsWithHeaderTypes: FirstPartyHosts = ["first-party.com": .init(arrayLiteral: .dd)] // When let tracingEnabled = false @@ -750,7 +753,11 @@ class FeaturesConfigurationTests: XCTestCase { // Then let configuration = try FeaturesConfiguration( - configuration: .mockWith(tracingEnabled: tracingEnabled, rumEnabled: rumEnabled, firstPartyHosts: firstPartyHosts), + configuration: .mockWith( + tracingEnabled: tracingEnabled, + rumEnabled: rumEnabled, + firstPartyHostsWithHeaderTypes: firstPartyHostsWithHeaderTypes + ), appContext: .mockAny() ) @@ -770,23 +777,23 @@ class FeaturesConfigurationTests: XCTestCase { func testWhenFirstPartyHostsAreProvided_itPassesThemToSanitizer() throws { // When - let firstPartyHosts: Set = [ - "https://first-party.com", - "http://api.first-party.com", - "https://first-party.com/v2/api" + let firstPartyHostsWithHeaderTypes: FirstPartyHosts = [ + "https://first-party.com": .init(arrayLiteral: .dd), + "http://api.first-party.com": .init(arrayLiteral: .dd), + "https://first-party.com/v2/api": .init(arrayLiteral: .dd) ] // Then let mockHostsSanitizer = MockHostsSanitizer() _ = try FeaturesConfiguration( - configuration: .mockWith(rumEnabled: true, firstPartyHosts: firstPartyHosts), + configuration: .mockWith(rumEnabled: true, firstPartyHostsWithHeaderTypes: firstPartyHostsWithHeaderTypes), appContext: .mockAny(), hostsSanitizer: mockHostsSanitizer ) XCTAssertEqual(mockHostsSanitizer.sanitizations.count, 1) let sanitization = try XCTUnwrap(mockHostsSanitizer.sanitizations.first) - XCTAssertEqual(sanitization.hosts, firstPartyHosts) + XCTAssertEqual(sanitization.hosts, firstPartyHostsWithHeaderTypes.hosts) XCTAssertEqual(sanitization.warningMessage, "The first party host configured for Datadog SDK is not valid") } diff --git a/Tests/DatadogTests/Datadog/DatadogConfigurationBuilderTests.swift b/Tests/DatadogTests/Datadog/DatadogConfigurationBuilderTests.swift index db00ae83cb..91b175c4a7 100644 --- a/Tests/DatadogTests/Datadog/DatadogConfigurationBuilderTests.swift +++ b/Tests/DatadogTests/Datadog/DatadogConfigurationBuilderTests.swift @@ -42,7 +42,7 @@ class DatadogConfigurationBuilderTests: XCTestCase { XCTAssertEqual(configuration.tracesEndpoint, .us1) XCTAssertEqual(configuration.rumEndpoint, .us1) XCTAssertNil(configuration.serviceName) - XCTAssertNil(configuration.firstPartyHosts) + XCTAssertNil(configuration.firstPartyHostsWithHeaderTypes) XCTAssertNil(configuration.logEventMapper) XCTAssertNil(configuration.spanEventMapper) XCTAssertEqual(configuration.loggingSamplingRate, 100.0) @@ -154,7 +154,7 @@ class DatadogConfigurationBuilderTests: XCTestCase { XCTAssertEqual(configuration.customLogsEndpoint, URL(string: "https://api.custom.logs/")!) XCTAssertEqual(configuration.customTracesEndpoint, URL(string: "https://api.custom.traces/")!) XCTAssertEqual(configuration.customRUMEndpoint, URL(string: "https://api.custom.rum/")!) - XCTAssertEqual(configuration.firstPartyHosts, ["example.com"]) + XCTAssertEqual(configuration.firstPartyHostsWithHeaderTypes, ["example.com": .init(arrayLiteral: .dd)]) XCTAssertEqual(configuration.loggingSamplingRate, 66) XCTAssertEqual(configuration.tracingSamplingRate, 75) XCTAssertEqual(configuration.rumSessionsSamplingRate, 42.5) @@ -202,7 +202,7 @@ class DatadogConfigurationBuilderTests: XCTestCase { let configuration = builder.build() - XCTAssertEqual(configuration.firstPartyHosts, ["example.com"]) + XCTAssertEqual(configuration.firstPartyHostsWithHeaderTypes, ["example.com": .init(arrayLiteral: .dd)]) XCTAssertEqual(configuration.logsEndpoint, .eu1) XCTAssertEqual(configuration.tracesEndpoint, .eu1) XCTAssertEqual(configuration.rumEndpoint, .eu1) diff --git a/Tests/DatadogTests/Datadog/Mocks/CoreMocks.swift b/Tests/DatadogTests/Datadog/Mocks/CoreMocks.swift index c335d5429d..aaf4ac307b 100644 --- a/Tests/DatadogTests/Datadog/Mocks/CoreMocks.swift +++ b/Tests/DatadogTests/Datadog/Mocks/CoreMocks.swift @@ -49,7 +49,7 @@ extension Datadog.Configuration { tracesEndpoint: TracesEndpoint = .us1, rumEndpoint: RUMEndpoint = .us1, serviceName: String? = .mockAny(), - firstPartyHosts: Set? = nil, + firstPartyHostsWithHeaderTypes: FirstPartyHosts? = nil, loggingSamplingRate: Float = 100.0, tracingSamplingRate: Float = 100.0, rumSessionsSamplingRate: Float = 100.0, @@ -83,7 +83,7 @@ extension Datadog.Configuration { tracesEndpoint: tracesEndpoint, rumEndpoint: rumEndpoint, serviceName: serviceName, - firstPartyHosts: firstPartyHosts, + firstPartyHostsWithHeaderTypes: firstPartyHostsWithHeaderTypes, loggingSamplingRate: loggingSamplingRate, tracingSamplingRate: tracingSamplingRate, rumSessionsSamplingRate: rumSessionsSamplingRate, @@ -337,8 +337,7 @@ extension FeaturesConfiguration.URLSessionAutoInstrumentation { static func mockAny() -> Self { mockWith() } static func mockWith( - userDefinedFirstPartyHosts: Set = [], - userDefinedHostsWithHeaderTypes: [String: Set] = [:], + userDefinedHostsWithHeaderTypes: FirstPartyHosts = [:], sdkInternalURLs: Set = [], rumAttributesProvider: URLSessionRUMAttributesProvider? = nil, instrumentTracing: Bool = true, @@ -346,7 +345,6 @@ extension FeaturesConfiguration.URLSessionAutoInstrumentation { tracingSampler: Sampler = .mockKeepAll() ) -> Self { return .init( - userDefinedFirstPartyHosts: userDefinedFirstPartyHosts, userDefinedHostsWithHeaderTypes: userDefinedHostsWithHeaderTypes, sdkInternalURLs: sdkInternalURLs, rumAttributesProvider: rumAttributesProvider, @@ -1121,13 +1119,13 @@ class MockHostsSanitizer: HostsSanitizing { return hosts } - private(set) var sanitizationsWithHeaderTypes = [(hostsWithHeaderTypes: [String: Set], warningMessage: String)]() + private(set) var sanitizationsWithHeaderTypes = [(firstPartyHosts: FirstPartyHosts, warningMessage: String)]() func sanitized( - hostsWithHeaderTypes: [String: Set], + firstPartyHosts: FirstPartyHosts, warningMessage: String - ) -> [String: Set] { - sanitizationsWithHeaderTypes.append((hostsWithHeaderTypes: hostsWithHeaderTypes, warningMessage: warningMessage)) - return hostsWithHeaderTypes + ) -> FirstPartyHosts { + sanitizationsWithHeaderTypes.append((firstPartyHosts: firstPartyHosts, warningMessage: warningMessage)) + return firstPartyHosts } } diff --git a/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/TracingHeaderTypesProvider/TracingHeaderTypesProviderTests.swift b/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/TracingHeaderTypesProvider/TracingHeaderTypesProviderTests.swift index 07981acb69..64fa45e5ec 100644 --- a/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/TracingHeaderTypesProvider/TracingHeaderTypesProviderTests.swift +++ b/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/TracingHeaderTypesProvider/TracingHeaderTypesProviderTests.swift @@ -8,7 +8,7 @@ import XCTest @testable import Datadog class TracingHeaderTypesProviderTests: XCTestCase { - let hostsWithHeaderTypes: [String: Set] = [ + let firstPartyHosts: FirstPartyHosts = [ "http://first-party.com/": .init(arrayLiteral: .w3c, .b3s), "https://first-party.com/": .init(arrayLiteral: .w3c, .b3s), "https://api.first-party.com/v2/users": .init(arrayLiteral: .w3c, .b3s), @@ -38,39 +38,42 @@ class TracingHeaderTypesProviderTests: XCTestCase { func test_TracingHeaderTypesProviderWithEmptyDictionary_itReturnsDefaultTracingHeaderTypes() { let headerTypesProvider = TracingHeaderTypesProvider( - hostsWithHeaderTypes: [:] + firstPartyHosts: [:] ) - (hostsWithHeaderTypes.keys + otherHosts).forEach { fixture in + (firstPartyHosts.keys + otherHosts).forEach { fixture in let url = URL(string: fixture) - XCTAssertEqual(headerTypesProvider.tracingHeaderTypes(for: url), .init(arrayLiteral: .dd)) + XCTAssertEqual(headerTypesProvider.tracingHeaderTypes(for: url), .init()) } } func test_TracingHeaderTypesProviderWithEmptyTracingHeaderTypes_itReturnsNoTracingHeaderTypes() { let headerTypesProvider = TracingHeaderTypesProvider( - hostsWithHeaderTypes: ["http://first-party.com/": .init()] + firstPartyHosts: ["http://first-party.com/": .init()] ) XCTAssertEqual(headerTypesProvider.tracingHeaderTypes(for: URL(string: "http://first-party.com/")), .init()) } func test_TracingHeaderTypesProviderWithValidDictionary_itReturnsTracingHeaderTypes_forSubdomainURL() { let headerTypesProvider = TracingHeaderTypesProvider( - hostsWithHeaderTypes: ["first-party.com/": .init(arrayLiteral: .b3m)] + firstPartyHosts: ["first-party.com": .init(arrayLiteral: .b3m)] ) - XCTAssertEqual(headerTypesProvider.tracingHeaderTypes(for: URL(string: "api.first-party.com/")), .init(arrayLiteral: .b3m)) + XCTAssertEqual(headerTypesProvider.tracingHeaderTypes(for: URL(string: "api.first-party.com")), .init(arrayLiteral: .b3m)) + XCTAssertEqual(headerTypesProvider.tracingHeaderTypes(for: URL(string: "apifirst-party.com")), .init()) + XCTAssertEqual(headerTypesProvider.tracingHeaderTypes(for: URL(string: "https://api.first-party.com/v1/endpoint")), .init(arrayLiteral: .b3m)) + } func test_TracingHeaderTypesProviderWithValidDictionary_itReturnsCorrectTracingHeaderTypes() { let headerTypesProvider = TracingHeaderTypesProvider( - hostsWithHeaderTypes: hostsWithHeaderTypes + firstPartyHosts: firstPartyHosts ) - hostsWithHeaderTypes.keys.forEach { fixture in + firstPartyHosts.keys.forEach { fixture in let url = URL(string: fixture) XCTAssertEqual(headerTypesProvider.tracingHeaderTypes(for: url), .init(arrayLiteral: .w3c, .b3s)) } otherHosts.forEach { fixture in let url = URL(string: fixture) - XCTAssertEqual(headerTypesProvider.tracingHeaderTypes(for: url), .init(arrayLiteral: .dd)) + XCTAssertEqual(headerTypesProvider.tracingHeaderTypes(for: url), .init()) } } } diff --git a/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/URLFiltering/FirstPartyURLsFilterTests.swift b/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/URLFiltering/FirstPartyURLsFilterTests.swift index 82327618fc..343ca58f05 100644 --- a/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/URLFiltering/FirstPartyURLsFilterTests.swift +++ b/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/URLFiltering/FirstPartyURLsFilterTests.swift @@ -78,4 +78,26 @@ class FirstPartyURLsFilterTests: XCTestCase { } } } + + func testWhenURLHostIsSubdomain_itIsConsideredFirstParty() { + let filter = FirstPartyURLsFilter( + hosts: ["first-party.com"] + ) + let url = URL(string: "https://api.first-party.com")! + XCTAssertTrue( + filter.isFirstParty(url: url), + "The url: `\(url)` should NOT be matched as first party." + ) + } + + func testWhenURLHostIsNotSubdomain_itIsNotConsideredFirstParty() { + let filter = FirstPartyURLsFilter( + hosts: ["first-party.com"] + ) + let url = URL(string: "https://apifirst-party.com")! + XCTAssertFalse( + filter.isFirstParty(url: url), + "The url: `\(url)` should NOT be matched as first party." + ) + } } diff --git a/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/URLSessionInterceptorTests.swift b/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/URLSessionInterceptorTests.swift index 71e83c8490..9a21c4859b 100644 --- a/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/URLSessionInterceptorTests.swift +++ b/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/URLSessionInterceptorTests.swift @@ -117,7 +117,6 @@ class URLSessionInterceptorTests: XCTestCase { tracingSampler: Sampler = .mockKeepAll() ) -> FeaturesConfiguration.URLSessionAutoInstrumentation { return .mockWith( - userDefinedFirstPartyHosts: ["first-party.com"], userDefinedHostsWithHeaderTypes: ["first-party.com": .init(arrayLiteral: .dd)], sdkInternalURLs: ["https://dd.internal.com"], instrumentTracing: tracingInstrumentationEnabled, diff --git a/Tests/DatadogTests/DatadogObjc/DDConfigurationTests.swift b/Tests/DatadogTests/DatadogObjc/DDConfigurationTests.swift index b036a65c85..0db0850b39 100644 --- a/Tests/DatadogTests/DatadogObjc/DDConfigurationTests.swift +++ b/Tests/DatadogTests/DatadogObjc/DDConfigurationTests.swift @@ -53,7 +53,7 @@ class DDConfigurationTests: XCTestCase { XCTAssertEqual(configuration.rumEndpoint, .us1) XCTAssertEqual(configuration.environment, "tests") XCTAssertNil(configuration.serviceName) - XCTAssertNil(configuration.firstPartyHosts) + XCTAssertNil(configuration.firstPartyHostsWithHeaderTypes) XCTAssertEqual(configuration.rumSessionsSamplingRate, 100.0) XCTAssertNil(configuration.rumUIKitViewsPredicate) XCTAssertNil(configuration.rumUIKitUserActionsPredicate) @@ -136,7 +136,7 @@ class DDConfigurationTests: XCTestCase { XCTAssertEqual(objcBuilder.build().sdkConfiguration.serviceName, "service-name") objcBuilder.trackURLSession(firstPartyHosts: ["example.com"]) - XCTAssertEqual(objcBuilder.build().sdkConfiguration.firstPartyHosts, ["example.com"]) + XCTAssertEqual(objcBuilder.build().sdkConfiguration.firstPartyHostsWithHeaderTypes?.hosts, ["example.com"]) objcBuilder.set(loggingSamplingRate: 66) XCTAssertEqual(objcBuilder.build().sdkConfiguration.loggingSamplingRate, 66)