diff --git a/Sources/EmbraceCore/Capture/Network/URLSessionCaptureService+Options.swift b/Sources/EmbraceCore/Capture/Network/URLSessionCaptureService+Options.swift index 068a9e8e..2ce83bc6 100644 --- a/Sources/EmbraceCore/Capture/Network/URLSessionCaptureService+Options.swift +++ b/Sources/EmbraceCore/Capture/Network/URLSessionCaptureService+Options.swift @@ -15,13 +15,18 @@ extension URLSessionCaptureService { /// before the Embrace SDK captures their data. @objc public let requestsDataSource: URLSessionRequestsDataSource? - @objc public init(injectTracingHeader: Bool, requestsDataSource: URLSessionRequestsDataSource?) { + /// List of urls to be ignored by this service. + /// Any request's url that contains any of these strings will not be captured. + @objc public let ignoredURLs: [String] + + @objc public init(injectTracingHeader: Bool, requestsDataSource: URLSessionRequestsDataSource?, ignoredURLs: [String]) { self.injectTracingHeader = injectTracingHeader self.requestsDataSource = requestsDataSource + self.ignoredURLs = ignoredURLs } @objc public convenience override init() { - self.init(injectTracingHeader: true, requestsDataSource: nil) + self.init(injectTracingHeader: true, requestsDataSource: nil, ignoredURLs: []) } } } diff --git a/Sources/EmbraceCore/Capture/Network/URLSessionCaptureService.swift b/Sources/EmbraceCore/Capture/Network/URLSessionCaptureService.swift index e65a8f70..95c508e1 100644 --- a/Sources/EmbraceCore/Capture/Network/URLSessionCaptureService.swift +++ b/Sources/EmbraceCore/Capture/Network/URLSessionCaptureService.swift @@ -80,6 +80,10 @@ public final class URLSessionCaptureService: CaptureService, URLSessionTaskHandl var requestsDataSource: URLSessionRequestsDataSource? { return options.requestsDataSource } + + var ignoredURLs: [String] { + return options.ignoredURLs + } } // swiftlint:disable line_length diff --git a/Sources/EmbraceCore/Capture/Network/URLSessionTaskHandler.swift b/Sources/EmbraceCore/Capture/Network/URLSessionTaskHandler.swift index 1dcbdb70..0d943142 100644 --- a/Sources/EmbraceCore/Capture/Network/URLSessionTaskHandler.swift +++ b/Sources/EmbraceCore/Capture/Network/URLSessionTaskHandler.swift @@ -26,6 +26,7 @@ protocol URLSessionTaskHandlerDataSource: AnyObject { var injectTracingHeader: Bool { get } var requestsDataSource: URLSessionRequestsDataSource? { get } + var ignoredURLs: [String] { get } } final class DefaultURLSessionTaskHandler: URLSessionTaskHandler { @@ -69,6 +70,11 @@ final class DefaultURLSessionTaskHandler: URLSessionTaskHandler { return } + // check ignored urls + guard shouldCapture(url: url) else { + return + } + // get modified request from data source request = self.dataSource?.requestsDataSource?.modifiedRequest(for: request) ?? request @@ -211,6 +217,20 @@ final class DefaultURLSessionTaskHandler: URLSessionTaskHandler { return nil } + + func shouldCapture(url: URL) -> Bool { + guard let list = dataSource?.ignoredURLs else { + return true + } + + for str in list { + if url.absoluteString.contains(str) { + return false + } + } + + return true + } } private extension DefaultURLSessionTaskHandler { diff --git a/Tests/EmbraceCoreTests/Capture/Network/DefaultURLSessionTaskHandlerTests.swift b/Tests/EmbraceCoreTests/Capture/Network/DefaultURLSessionTaskHandlerTests.swift index bb52c473..0653c0ab 100644 --- a/Tests/EmbraceCoreTests/Capture/Network/DefaultURLSessionTaskHandlerTests.swift +++ b/Tests/EmbraceCoreTests/Capture/Network/DefaultURLSessionTaskHandlerTests.swift @@ -18,6 +18,7 @@ class MockURLSessionTaskHandlerDataSource: URLSessionTaskHandlerDataSource { var injectTracingHeader = false var requestsDataSource: URLSessionRequestsDataSource? + var ignoredURLs: [String] = [] } class MockURLSessionRequestsDataSource: NSObject, URLSessionRequestsDataSource { @@ -43,6 +44,7 @@ class DefaultURLSessionTaskHandlerTests: XCTestCase { dataSource = MockURLSessionTaskHandlerDataSource() dataSource.otel = otel + dataSource.ignoredURLs = [] } // MARK: - Create Tests @@ -202,6 +204,25 @@ class DefaultURLSessionTaskHandlerTests: XCTestCase { whenInvokingFinish() thenSpanHasTheCorrectBodySize(4) } + + func test_ignoredURLs_matches() { + givenTaskHandler() + givenIgnoredURLs() + givenAnURLSessionTask() + whenInvokingCreate(withoutWaiting: true) + + wait { + return self.otel.spanProcessor.startedSpans.count == 0 + } + } + + func test_ignoredURLs_no_match() { + givenTaskHandler() + givenIgnoredURLs() + givenAnURLSessionTask(urlString: "https://ThisIsAUrl/with/some/path") + whenInvokingCreate() + thenHTTPNetworkSpanShouldBeCreated() + } } private extension DefaultURLSessionTaskHandlerTests { @@ -224,6 +245,10 @@ private extension DefaultURLSessionTaskHandlerTests { dataSource.requestsDataSource = requestsDataSource } + func givenIgnoredURLs() { + dataSource.ignoredURLs = ["embrace.io"] + } + func givenHandlerCreatedASpan(withResponse response: URLResponse? = nil) { givenAnURLSessionTask(response: response) sut.create(task: task) diff --git a/Tests/EmbraceIOTests/CaptureServiceBuilderTests.swift b/Tests/EmbraceIOTests/CaptureServiceBuilderTests.swift index f0899e77..8b2be1af 100644 --- a/Tests/EmbraceIOTests/CaptureServiceBuilderTests.swift +++ b/Tests/EmbraceIOTests/CaptureServiceBuilderTests.swift @@ -46,7 +46,7 @@ class CaptureServiceBuilderTests: XCTestCase { let builder = CaptureServiceBuilder() // when adding a URLSessionCaptureService with custom options - let options = URLSessionCaptureService.Options(injectTracingHeader: false, requestsDataSource: nil) + let options = URLSessionCaptureService.Options(injectTracingHeader: false, requestsDataSource: nil, ignoredURLs: []) builder.add(.urlSession(options: options)) // when adding the defaults @@ -117,7 +117,7 @@ class CaptureServiceBuilderTests: XCTestCase { builder.add(.urlSession()) // and then adding it again - let options = URLSessionCaptureService.Options(injectTracingHeader: false, requestsDataSource: nil) + let options = URLSessionCaptureService.Options(injectTracingHeader: false, requestsDataSource: nil, ignoredURLs: []) builder.add(.urlSession(options: options)) // then the list contains the correct services @@ -134,7 +134,7 @@ class CaptureServiceBuilderTests: XCTestCase { let builder = CaptureServiceBuilder() // when adding a URLSessionCaptureService - let options = URLSessionCaptureService.Options(injectTracingHeader: false, requestsDataSource: nil) + let options = URLSessionCaptureService.Options(injectTracingHeader: false, requestsDataSource: nil, ignoredURLs: []) builder.add(.urlSession(options: options)) // then the list contains the capture service