From 34d83b57a5985f72dd2902813ce5297d1acaa256 Mon Sep 17 00:00:00 2001 From: Bryce Buchanan Date: Thu, 21 Nov 2024 08:40:59 -0800 Subject: [PATCH 1/2] added other AV task exceptions in NSURLSession instrumentation --- .../URLSessionInstrumentation.swift | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift b/Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift index a67920eb..7ee19de8 100644 --- a/Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift +++ b/Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift @@ -26,16 +26,25 @@ struct NetworkRequestState { private var idKey: Void? public class URLSessionInstrumentation { - private var requestMap = [String: NetworkRequestState]() - - var configuration: URLSessionInstrumentationConfiguration - - private let queue = DispatchQueue(label: "io.opentelemetry.ddnetworkinstrumentation") - - static var instrumentedKey = "io.opentelemetry.instrumentedCall" - - static let avAssetDownloadTask: AnyClass? = NSClassFromString("__NSCFBackgroundAVAssetDownloadTask") - + private var requestMap = [String: NetworkRequestState]() + + var configuration: URLSessionInstrumentationConfiguration + + private let queue = DispatchQueue(label: "io.opentelemetry.ddnetworkinstrumentation") + + static var instrumentedKey = "io.opentelemetry.instrumentedCall" + + static let AVTaskClassList : [AnyClass] = { + let names = ["__NSCFBackgroundAVAggregateAssetDownloadTask", "__NSCFBackgroundAVAssetDownloadTask", "__NSCFBackgroundAVAggregateAssetDownloadTaskNoChildTask" ] + var classes : [AnyClass] = [] + for name in names { + if let aClass = NSClassFromString(name) { + classes.append(aClass) + } + } + return classes + }() + public private(set) var tracer: Tracer public var startedRequestSpans: [Span] { @@ -592,9 +601,10 @@ public class URLSessionInstrumentation { private func urlSessionTaskWillResume(_ task: URLSessionTask) { // AV Asset Tasks cannot be auto instrumented, they dont include request attributes, skip them - if let avAssetTaskClass = Self.avAssetDownloadTask, - task.isKind(of: avAssetTaskClass) { - return + for aClass in Self.AVTaskClassList { + if task.isKind(of: aClass) { + return + } } // We cannot instrument async background tasks because they crash if you assign a delegate From b799e432637acc39a23bf91f3732f59fc82ed5af Mon Sep 17 00:00:00 2001 From: Bryce Buchanan Date: Thu, 21 Nov 2024 09:29:22 -0800 Subject: [PATCH 2/2] tidied syntax --- .../URLSession/URLSessionInstrumentation.swift | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift b/Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift index 7ee19de8..20714ea6 100644 --- a/Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift +++ b/Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift @@ -35,14 +35,10 @@ public class URLSessionInstrumentation { static var instrumentedKey = "io.opentelemetry.instrumentedCall" static let AVTaskClassList : [AnyClass] = { - let names = ["__NSCFBackgroundAVAggregateAssetDownloadTask", "__NSCFBackgroundAVAssetDownloadTask", "__NSCFBackgroundAVAggregateAssetDownloadTaskNoChildTask" ] - var classes : [AnyClass] = [] - for name in names { - if let aClass = NSClassFromString(name) { - classes.append(aClass) - } - } - return classes + ["__NSCFBackgroundAVAggregateAssetDownloadTask", + "__NSCFBackgroundAVAssetDownloadTask", + "__NSCFBackgroundAVAggregateAssetDownloadTaskNoChildTask" ] + .compactMap { NSClassFromString($0) } }() public private(set) var tracer: Tracer @@ -601,11 +597,7 @@ public class URLSessionInstrumentation { private func urlSessionTaskWillResume(_ task: URLSessionTask) { // AV Asset Tasks cannot be auto instrumented, they dont include request attributes, skip them - for aClass in Self.AVTaskClassList { - if task.isKind(of: aClass) { - return - } - } + guard !Self.AVTaskClassList.contains(where: {task.isKind(of:$0)}) else { return } // We cannot instrument async background tasks because they crash if you assign a delegate if #available(OSX 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {