Skip to content

Commit 97dfbe1

Browse files
author
Ignacio Bonafonte
committed
URLSession instrumentation: Fix custom request manipulation
Separate the possibility to modify URLRequest to another `injectCustomHeaders` callback, `shouldInjectTracingHeaders` can be called more than once, and if the request was being modified in the method it would happen twice.
1 parent e5a9692 commit 97dfbe1

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

Sources/Instrumentation/URLSession/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ This behaviour can be modified or augmented by using the optional callbacks defi
1313

1414
`shouldRecordPayload: ((URLSession) -> (Bool)?)?`: Implement if you want the session to record payload data, false by default.
1515

16-
`shouldInjectTracingHeaders: ((inout URLRequest) -> (Bool)?)?`: Allows filtering which requests you want to inject headers to follow the trace, true by default. You can also modify the request or add other headers in this method.
16+
`shouldInjectTracingHeaders: ((URLRequest) -> (Bool)?)?`: Allows filtering which requests you want to inject headers to follow the trace, true by default. You must also return true if you want to inject custom headers.
17+
18+
`injectCustomHeaders: ((inout URLRequest, Span?) -> Void)?`: Implement this callback to inject custom headers or modify the request in any other way
1719

1820
`nameSpan: ((URLRequest) -> (String)?)?` - Modifies the name for the given request instead of stantard Opentelemetry name
1921

Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ public class URLSessionInstrumentation {
107107

108108
let block: @convention(block) (URLSession, AnyObject) -> URLSessionTask = { session, argument in
109109
if let url = argument as? URL {
110-
var request = URLRequest(url: url)
111-
if self.configuration.shouldInjectTracingHeaders?(&request) ?? true {
110+
let request = URLRequest(url: url)
111+
if self.configuration.shouldInjectTracingHeaders?(request) ?? true {
112112
if selector == #selector(URLSession.dataTask(with:) as (URLSession) -> (URL) -> URLSessionDataTask) {
113113
return session.dataTask(with: request)
114114
} else {
@@ -184,9 +184,9 @@ public class URLSessionInstrumentation {
184184
let block: @convention(block) (URLSession, AnyObject, ((Any?, URLResponse?, Error?) -> Void)?) -> URLSessionTask = { session, argument, completion in
185185

186186
if let url = argument as? URL {
187-
var request = URLRequest(url: url)
187+
let request = URLRequest(url: url)
188188

189-
if self.configuration.shouldInjectTracingHeaders?(&request) ?? true {
189+
if self.configuration.shouldInjectTracingHeaders?(request) ?? true {
190190
if selector == #selector(URLSession.dataTask(with:completionHandler:) as (URLSession) -> (URL, @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask) {
191191
if let completion = completion {
192192
return session.dataTask(with: request, completionHandler: completion)

Sources/Instrumentation/URLSession/URLSessionInstrumentationConfiguration.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ public struct URLSessionInstrumentationConfiguration {
1515
public init(shouldRecordPayload: ((URLSession) -> (Bool)?)? = nil,
1616
shouldInstrument: ((URLRequest) -> (Bool)?)? = nil,
1717
nameSpan: ((URLRequest) -> (String)?)? = nil,
18-
shouldInjectTracingHeaders: ((inout URLRequest) -> (Bool)?)? = nil,
18+
shouldInjectTracingHeaders: ((URLRequest) -> (Bool)?)? = nil,
19+
injectCustomHeaders: ((inout URLRequest, Span?) -> Void)? = nil,
1920
createdRequest: ((URLRequest, Span) -> Void)? = nil,
2021
receivedResponse: ((URLResponse, DataOrFile?, Span) -> Void)? = nil,
2122
receivedError: ((Error, DataOrFile?, HTTPStatus, Span) -> Void)? = nil)
2223
{
2324
self.shouldRecordPayload = shouldRecordPayload
2425
self.shouldInstrument = shouldInstrument
2526
self.shouldInjectTracingHeaders = shouldInjectTracingHeaders
27+
self.injectCustomHeaders = injectCustomHeaders
2628
self.nameSpan = nameSpan
2729
self.createdRequest = createdRequest
2830
self.receivedResponse = receivedResponse
@@ -39,9 +41,12 @@ public struct URLSessionInstrumentationConfiguration {
3941
public var shouldRecordPayload: ((URLSession) -> (Bool)?)?
4042

4143
/// Implement this callback to filter which requests you want to inject headers to follow the trace,
42-
/// you can also modify the request or add other headers in this method.
44+
/// also must implement it if you want to inject custom headers
4345
/// Instruments all requests by default
44-
public var shouldInjectTracingHeaders: ((inout URLRequest) -> (Bool)?)?
46+
public var shouldInjectTracingHeaders: ((URLRequest) -> (Bool)?)?
47+
48+
/// Implement this callback to inject custom headers or modify the request in any other way
49+
public var injectCustomHeaders: ((inout URLRequest, Span?) -> Void)?
4550

4651
/// Implement this callback to override the default span name for a given request, return nil to use default.
4752
/// default name: `HTTP {method}` e.g. `HTTP PUT`

Sources/Instrumentation/URLSession/URLSessionLogger.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,11 @@ class URLSessionLogger {
138138

139139
private static func instrumentedRequest(for request: URLRequest, span: Span?, instrumentation: URLSessionInstrumentation) -> URLRequest? {
140140
var request = request
141-
guard instrumentation.configuration.shouldInjectTracingHeaders?(&request) ?? true
141+
guard instrumentation.configuration.shouldInjectTracingHeaders?(request) ?? true
142142
else {
143143
return nil
144144
}
145+
instrumentation.configuration.injectCustomHeaders?(&request, span)
145146
var instrumentedRequest = request
146147
objc_setAssociatedObject(instrumentedRequest, &URLSessionInstrumentation.instrumentedKey, true, .OBJC_ASSOCIATION_COPY_NONATOMIC)
147148
var traceHeaders = tracePropagationHTTPHeaders(span: span, textMapPropagator: instrumentation.tracer.textFormat)

0 commit comments

Comments
 (0)