Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EMBR-5747] Adding url ignore list to URLSessionCaptureService #134

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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: [])
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions Sources/EmbraceCore/Capture/Network/URLSessionTaskHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protocol URLSessionTaskHandlerDataSource: AnyObject {

var injectTracingHeader: Bool { get }
var requestsDataSource: URLSessionRequestsDataSource? { get }
var ignoredURLs: [String] { get }
}

final class DefaultURLSessionTaskHandler: URLSessionTaskHandler {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MockURLSessionTaskHandlerDataSource: URLSessionTaskHandlerDataSource {

var injectTracingHeader = false
var requestsDataSource: URLSessionRequestsDataSource?
var ignoredURLs: [String] = []
}

class MockURLSessionRequestsDataSource: NSObject, URLSessionRequestsDataSource {
Expand All @@ -43,6 +44,7 @@ class DefaultURLSessionTaskHandlerTests: XCTestCase {

dataSource = MockURLSessionTaskHandlerDataSource()
dataSource.otel = otel
dataSource.ignoredURLs = []
}

// MARK: - Create Tests
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions Tests/EmbraceIOTests/CaptureServiceBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading