Skip to content

Commit c0cef3c

Browse files
author
Ignacio Bonafonte
authored
Merge pull request #270 from nachoBonafonte/fix-network-instrumentation
Fix URLSessionInstrumentation concurrent network requests URLSessionInstrumentation was not reporting concurrent network requests The task identifier was being set out of the callback implementation so it was being reused, if the session tried to send concurrent requests, only the first one was being reported.
2 parents 1a43c39 + 905d013 commit c0cef3c

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ public class URLSessionInstrumentation {
114114
return
115115
}
116116
var originalIMP: IMP?
117-
let sessionTaskId = UUID().uuidString
118117

119118
let block: @convention(block) (URLSession, AnyObject) -> URLSessionTask = { session, argument in
120119
if let url = argument as? URL {
@@ -130,6 +129,7 @@ public class URLSessionInstrumentation {
130129

131130
let castedIMP = unsafeBitCast(originalIMP, to: (@convention(c) (URLSession, Selector, Any) -> URLSessionDataTask).self)
132131
var task: URLSessionTask
132+
let sessionTaskId = UUID().uuidString
133133

134134
if let request = argument as? URLRequest, objc_getAssociatedObject(argument, &idKey) == nil {
135135
let instrumentedRequest = URLSessionLogger.processAndLogRequest(request, sessionTaskId: sessionTaskId, instrumentation: self, shouldInjectHeaders: true)
@@ -161,9 +161,9 @@ public class URLSessionInstrumentation {
161161
return
162162
}
163163
var originalIMP: IMP?
164-
let sessionTaskId = UUID().uuidString
165164

166165
let block: @convention(block) (URLSession, URLRequest, AnyObject) -> URLSessionTask = { session, request, argument in
166+
let sessionTaskId = UUID().uuidString
167167
let castedIMP = unsafeBitCast(originalIMP, to: (@convention(c) (URLSession, Selector, URLRequest, AnyObject) -> URLSessionDataTask).self)
168168
let instrumentedRequest = URLSessionLogger.processAndLogRequest(request, sessionTaskId: sessionTaskId, instrumentation: self, shouldInjectHeaders: true)
169169
let task = castedIMP(session, selector, instrumentedRequest ?? request, argument)
@@ -190,7 +190,6 @@ public class URLSessionInstrumentation {
190190
return
191191
}
192192
var originalIMP: IMP?
193-
let sessionTaskId = UUID().uuidString
194193

195194
let block: @convention(block) (URLSession, AnyObject, ((Any?, URLResponse?, Error?) -> Void)?) -> URLSessionTask = { session, argument, completion in
196195

@@ -216,6 +215,7 @@ public class URLSessionInstrumentation {
216215

217216
let castedIMP = unsafeBitCast(originalIMP, to: (@convention(c) (URLSession, Selector, Any, ((Any?, URLResponse?, Error?) -> Void)?) -> URLSessionDataTask).self)
218217
var task: URLSessionTask!
218+
let sessionTaskId = UUID().uuidString
219219

220220
var completionBlock = completion
221221

@@ -242,11 +242,7 @@ public class URLSessionInstrumentation {
242242

243243
if let request = argument as? URLRequest, objc_getAssociatedObject(argument, &idKey) == nil {
244244
let instrumentedRequest = URLSessionLogger.processAndLogRequest(request, sessionTaskId: sessionTaskId, instrumentation: self, shouldInjectHeaders: true)
245-
if let instrumentedRequest = instrumentedRequest {
246-
task = castedIMP(session, selector, instrumentedRequest, completionBlock)
247-
} else {
248-
task = castedIMP(session, selector, request, completion)
249-
}
245+
task = castedIMP(session, selector, instrumentedRequest ?? request, completionBlock)
250246
} else {
251247
task = castedIMP(session, selector, argument, completionBlock)
252248
if objc_getAssociatedObject(argument, &idKey) == nil,
@@ -275,13 +271,14 @@ public class URLSessionInstrumentation {
275271
return
276272
}
277273
var originalIMP: IMP?
278-
let sessionTaskId = UUID().uuidString
279274

280275
let block: @convention(block) (URLSession, URLRequest, AnyObject, ((Any?, URLResponse?, Error?) -> Void)?) -> URLSessionTask = { session, request, argument, completion in
281276

282277
let castedIMP = unsafeBitCast(originalIMP, to: (@convention(c) (URLSession, Selector, URLRequest, AnyObject, ((Any?, URLResponse?, Error?) -> Void)?) -> URLSessionDataTask).self)
283278

284279
var task: URLSessionTask!
280+
let sessionTaskId = UUID().uuidString
281+
285282
var completionBlock = completion
286283
if objc_getAssociatedObject(argument, &idKey) == nil {
287284
let completionWrapper: (Any?, URLResponse?, Error?) -> Void = { object, response, error in

0 commit comments

Comments
 (0)