Skip to content

Commit 10563f4

Browse files
author
Ignacio Bonafonte
authored
URLSessionInstrumentation version check was wrong and it made fail instrumenting async-await network calls when running in certain system versions (open-telemetry#488)
1 parent f9704c1 commit 10563f4

File tree

2 files changed

+16
-42
lines changed

2 files changed

+16
-42
lines changed

Sources/Instrumentation/URLSession/InstrumentationUtils.swift

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
import Foundation
77
#if os(iOS) || os(tvOS)
8-
import UIKit
8+
import UIKit
99
#elseif os(watchOS)
10-
import WatchKit
10+
import WatchKit
1111
#endif
1212

1313
enum InstrumentationUtils {
@@ -53,28 +53,4 @@ enum InstrumentationUtils {
5353
ptr = ptr.successor()
5454
}
5555
}
56-
57-
static var usesUndocumentedAsyncAwaitMethods: Bool = {
58-
#if os(macOS)
59-
let os = ProcessInfo.processInfo.operatingSystemVersion
60-
if os.majorVersion >= 13 {
61-
return true
62-
}
63-
#elseif os(watchOS)
64-
let version = WKInterfaceDevice.current().systemVersion
65-
if let versionNumber = Double(version),
66-
versionNumber >= 9.0
67-
{
68-
return true
69-
}
70-
#else
71-
let version = UIDevice.current.systemVersion
72-
if let versionNumber = Double(version),
73-
versionNumber >= 16.0
74-
{
75-
return true
76-
}
77-
#endif
78-
return false
79-
}()
8056
}

Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class URLSessionInstrumentation {
3030
private let queue = DispatchQueue(label: "io.opentelemetry.ddnetworkinstrumentation")
3131

3232
static var instrumentedKey = "io.opentelemetry.instrumentedCall"
33-
33+
3434
static let avAssetDownloadTask: AnyClass? = NSClassFromString("__NSCFBackgroundAVAssetDownloadTask")
3535

3636
public private(set) var tracer: Tracer
@@ -107,7 +107,7 @@ public class URLSessionInstrumentation {
107107
injectTaskDidCompleteWithErrorIntoDelegateClass(cls: cls)
108108
injectRespondsToSelectorIntoDelegateClass(cls: cls)
109109
// For future use
110-
if InstrumentationUtils.usesUndocumentedAsyncAwaitMethods {
110+
if #available(OSX 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
111111
injectTaskDidFinishCollectingMetricsIntoDelegateClass(cls: cls)
112112
}
113113

@@ -552,7 +552,7 @@ public class URLSessionInstrumentation {
552552
guard let taskId = objc_getAssociatedObject(dataTask, &idKey) as? String else {
553553
return
554554
}
555-
self.setIdKey(value: taskId, for: downloadTask)
555+
setIdKey(value: taskId, for: downloadTask)
556556
}
557557

558558
private func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
@@ -587,7 +587,7 @@ public class URLSessionInstrumentation {
587587
task.isKind(of: avAssetTaskClass) {
588588
return
589589
}
590-
590+
591591
let taskId = idKeyForTask(task)
592592
if let request = task.currentRequest {
593593
queue.sync {
@@ -597,19 +597,17 @@ public class URLSessionInstrumentation {
597597
requestMap[taskId]?.setRequest(request)
598598
}
599599

600-
if InstrumentationUtils.usesUndocumentedAsyncAwaitMethods {
601-
if #available(OSX 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
602-
guard Task.basePriority != nil else {
603-
return
604-
}
605-
let instrumentedRequest = URLSessionLogger.processAndLogRequest(request, sessionTaskId: taskId, instrumentation: self, shouldInjectHeaders: true)
606-
task.setValue(instrumentedRequest, forKey: "currentRequest")
607-
self.setIdKey(value: taskId, for: task)
600+
if #available(OSX 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
601+
guard Task.basePriority != nil else {
602+
return
603+
}
604+
let instrumentedRequest = URLSessionLogger.processAndLogRequest(request, sessionTaskId: taskId, instrumentation: self, shouldInjectHeaders: true)
605+
task.setValue(instrumentedRequest, forKey: "currentRequest")
606+
self.setIdKey(value: taskId, for: task)
608607

609-
// If not inside a Task basePriority is nil
610-
if task.delegate == nil {
611-
task.delegate = FakeDelegate()
612-
}
608+
// If not inside a Task basePriority is nil
609+
if task.delegate == nil {
610+
task.delegate = FakeDelegate()
613611
}
614612
}
615613
}

0 commit comments

Comments
 (0)