diff --git a/Sources/Datadog/Tracing/AutoInstrumentation/URLSessionTracingHandler.swift b/Sources/Datadog/Tracing/AutoInstrumentation/URLSessionTracingHandler.swift index de61bc83a8..f656f3e5b7 100644 --- a/Sources/Datadog/Tracing/AutoInstrumentation/URLSessionTracingHandler.swift +++ b/Sources/Datadog/Tracing/AutoInstrumentation/URLSessionTracingHandler.swift @@ -56,8 +56,14 @@ internal class URLSessionTracingHandler: URLSessionInterceptionHandler { } let url = interception.request.url?.absoluteString ?? "unknown_url" + + if let requestUrl = interception.request.url { + var urlComponent = URLComponents(url: requestUrl, resolvingAgainstBaseURL: true) + urlComponent?.query = nil + let resourceUrl = urlComponent?.url?.absoluteString ?? "unknown_url" + span.setTag(key: DDTags.resource, value: resourceUrl) + } let method = interception.request.httpMethod ?? "unknown_method" - span.setTag(key: DDTags.resource, value: url) span.setTag(key: OTTags.httpUrl, value: url) span.setTag(key: OTTags.httpMethod, value: method) diff --git a/Tests/DatadogTests/Datadog/Mocks/SystemFrameworks/FoundationMocks.swift b/Tests/DatadogTests/Datadog/Mocks/SystemFrameworks/FoundationMocks.swift index 4d94890a3a..ddfccdd4c3 100644 --- a/Tests/DatadogTests/Datadog/Mocks/SystemFrameworks/FoundationMocks.swift +++ b/Tests/DatadogTests/Datadog/Mocks/SystemFrameworks/FoundationMocks.swift @@ -175,6 +175,12 @@ extension URL: AnyMockable, RandomMockable { return URL(string: "https://www.foo.com/")!.appendingPathComponent(pathComponent) } + static func mockWith(url: String, queryParams: [URLQueryItem]) -> URL { + var urlComponents = URLComponents(string: url) + urlComponents!.queryItems = queryParams + return urlComponents!.url! + } + static func mockRandom() -> URL { return URL(string: "https://www.foo.com/")! .appendingPathComponent( @@ -412,6 +418,13 @@ extension URLRequest: AnyMockable { request.httpMethod = httpMethod return request } + + static func mockWith(url: String, queryParams: [URLQueryItem], httpMethod: String) -> URLRequest { + let url: URL = .mockWith(url: url, queryParams: queryParams) + var request = URLRequest(url: url) + request.httpMethod = httpMethod + return request + } } // MARK: - Process diff --git a/Tests/DatadogTests/Datadog/Tracing/Autoinstrumentation/URLSessionTracingHandlerTests.swift b/Tests/DatadogTests/Datadog/Tracing/Autoinstrumentation/URLSessionTracingHandlerTests.swift index f458f330ad..4f89720a2a 100644 --- a/Tests/DatadogTests/Datadog/Tracing/Autoinstrumentation/URLSessionTracingHandlerTests.swift +++ b/Tests/DatadogTests/Datadog/Tracing/Autoinstrumentation/URLSessionTracingHandlerTests.swift @@ -113,7 +113,7 @@ class URLSessionTracingHandlerTests: XCTestCase { spanOutput.onSpanRecorded = { _ in spanSentExpectation.fulfill() } // Given - let request: URLRequest = .mockWith(httpMethod: "GET") + let request: URLRequest = .mockWith(url: "https://www.example.com", queryParams: [URLQueryItem(name: "foo", value: "42"), URLQueryItem(name: "lang", value: "en")], httpMethod: "GET") let error = NSError(domain: "domain", code: 123, userInfo: [NSLocalizedDescriptionKey: "network error"]) let interception = TaskInterception(request: request, isFirstParty: true) interception.register(completion: .mockWith(response: nil, error: error)) @@ -134,7 +134,7 @@ class URLSessionTracingHandlerTests: XCTestCase { let span = try XCTUnwrap(spanOutput.lastRecordedSpan) XCTAssertEqual(span.operationName, "urlsession.request") - XCTAssertEqual(span.resource, request.url!.absoluteString) + XCTAssertEqual(span.resource, "https://www.example.com") XCTAssertEqual(span.duration, 30) XCTAssertTrue(span.isError) XCTAssertEqual(span.tags[OTTags.httpUrl], request.url!.absoluteString)