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

RUMM-1910 Strip query parameters from span resource #728

Merged
merged 1 commit into from
Jan 21, 2022
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 @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
Comment on lines +422 to +427
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could simplify mocking by getting rid of func mockWith(url: String, queryParams: [URLQueryItem]) -> URL

and do

static func mockWith(url string: String, httpMethod: String = "GET") -> URLRequest {
    let url: URL = URL(string: string)!
    var request = URLRequest(url: url)
    request.httpMethod = httpMethod
    return request
}

let request: URLRequest = .mockWith(url: "https://www.example.com?foo=42&lang=en")

WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm always more inclined to have more control. Also, assuming we could use some fuzzy testing in iOS, it'd be easier to generate a random base url and a random list of query param to build the full url than writing all like you propose

}

// MARK: - Process
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand Down