Skip to content

Commit

Permalink
URLSessionInstrumentation version check was wrong and it made fail in…
Browse files Browse the repository at this point in the history
…strumenting async-await network calls when running in certain system versions
  • Loading branch information
nachoBonafonte committed Dec 3, 2023
1 parent f9704c1 commit 7cd1c7b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 42 deletions.
28 changes: 2 additions & 26 deletions Sources/Instrumentation/URLSession/InstrumentationUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import Foundation
#if os(iOS) || os(tvOS)
import UIKit
import UIKit
#elseif os(watchOS)
import WatchKit
import WatchKit
#endif

enum InstrumentationUtils {
Expand Down Expand Up @@ -53,28 +53,4 @@ enum InstrumentationUtils {
ptr = ptr.successor()
}
}

static var usesUndocumentedAsyncAwaitMethods: Bool = {
#if os(macOS)
let os = ProcessInfo.processInfo.operatingSystemVersion
if os.majorVersion >= 13 {
return true
}
#elseif os(watchOS)
let version = WKInterfaceDevice.current().systemVersion
if let versionNumber = Double(version),
versionNumber >= 9.0
{
return true
}
#else
let version = UIDevice.current.systemVersion
if let versionNumber = Double(version),
versionNumber >= 16.0
{
return true
}
#endif
return false
}()
}
30 changes: 14 additions & 16 deletions Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class URLSessionInstrumentation {
private let queue = DispatchQueue(label: "io.opentelemetry.ddnetworkinstrumentation")

static var instrumentedKey = "io.opentelemetry.instrumentedCall"

static let avAssetDownloadTask: AnyClass? = NSClassFromString("__NSCFBackgroundAVAssetDownloadTask")

public private(set) var tracer: Tracer
Expand Down Expand Up @@ -107,7 +107,7 @@ public class URLSessionInstrumentation {
injectTaskDidCompleteWithErrorIntoDelegateClass(cls: cls)
injectRespondsToSelectorIntoDelegateClass(cls: cls)
// For future use
if InstrumentationUtils.usesUndocumentedAsyncAwaitMethods {
if #available(OSX 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
injectTaskDidFinishCollectingMetricsIntoDelegateClass(cls: cls)
}

Expand Down Expand Up @@ -552,7 +552,7 @@ public class URLSessionInstrumentation {
guard let taskId = objc_getAssociatedObject(dataTask, &idKey) as? String else {
return
}
self.setIdKey(value: taskId, for: downloadTask)
setIdKey(value: taskId, for: downloadTask)
}

private func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
Expand Down Expand Up @@ -587,7 +587,7 @@ public class URLSessionInstrumentation {
task.isKind(of: avAssetTaskClass) {
return
}

let taskId = idKeyForTask(task)
if let request = task.currentRequest {
queue.sync {
Expand All @@ -597,19 +597,17 @@ public class URLSessionInstrumentation {
requestMap[taskId]?.setRequest(request)
}

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

// If not inside a Task basePriority is nil
if task.delegate == nil {
task.delegate = FakeDelegate()
}
// If not inside a Task basePriority is nil
if task.delegate == nil {
task.delegate = FakeDelegate()
}
}
}
Expand Down

0 comments on commit 7cd1c7b

Please sign in to comment.