diff --git a/Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift b/Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift index 21db9360..a67920eb 100644 --- a/Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift +++ b/Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift @@ -7,7 +7,7 @@ import Foundation import OpenTelemetryApi import OpenTelemetrySdk #if canImport(FoundationNetworking) -import FoundationNetworking + import FoundationNetworking #endif struct NetworkRequestState { @@ -162,6 +162,11 @@ public class URLSessionInstrumentation { } } self.setIdKey(value: sessionTaskId, for: task) + + // We want to identify background tasks + if session.configuration.identifier == nil { + objc_setAssociatedObject(task, "IsBackground", true, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) + } return task } let swizzledIMP = imp_implementationWithBlock(unsafeBitCast(block, to: AnyObject.self)) @@ -592,6 +597,15 @@ public class URLSessionInstrumentation { 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, *) { + if objc_getAssociatedObject(task, "IsBackground") is Bool { + guard Task.basePriority == nil else { + return + } + } + } + let taskId = idKeyForTask(task) if let request = task.currentRequest { queue.sync { diff --git a/Sources/OpenTelemetrySdk/Metrics/Stable/State/StableMeterSharedState.swift b/Sources/OpenTelemetrySdk/Metrics/Stable/State/StableMeterSharedState.swift index 718db240..f1d1768e 100644 --- a/Sources/OpenTelemetrySdk/Metrics/Stable/State/StableMeterSharedState.swift +++ b/Sources/OpenTelemetrySdk/Metrics/Stable/State/StableMeterSharedState.swift @@ -50,7 +50,7 @@ class StableMeterSharedState { var registeredStorages = [SynchronousMetricStorage]() for (reader, registry) in readerStorageRegisteries { for registeredView in reader.registry.findViews(descriptor: instrument, meterScope: instrumentationScope) { - if type(of: registeredView.view.aggregation) == DropAggregator.self { + if type(of: registeredView.view.aggregation) == DropAggregation.self { continue } registeredStorages.append(registry.register(newStorage: SynchronousMetricStorage.create(registeredReader: reader, registeredView: registeredView, descriptor: instrument, exemplarFilter: meterProviderSharedState.exemplarFilter)) as! SynchronousMetricStorage) @@ -66,7 +66,7 @@ class StableMeterSharedState { var registeredStorages = [AsynchronousMetricStorage]() for (reader, registry) in readerStorageRegisteries { for registeredView in reader.registry.findViews(descriptor: instrumentDescriptor, meterScope: instrumentationScope) { - if type(of: registeredView.view.aggregation) == DropAggregator.self { + if type(of: registeredView.view.aggregation) == DropAggregation.self { continue } registeredStorages.append(registry.register(newStorage: AsynchronousMetricStorage.create(registeredReader: reader, registeredView: registeredView, instrumentDescriptor: instrumentDescriptor)) as! AsynchronousMetricStorage)