Skip to content

Commit

Permalink
Merge branch 'main' into span-exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
nachoBonafonte authored Sep 19, 2024
2 parents 863d3f5 + b22dd41 commit f6182ae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Foundation
import OpenTelemetryApi
import OpenTelemetrySdk
#if canImport(FoundationNetworking)
import FoundationNetworking
import FoundationNetworking
#endif

struct NetworkRequestState {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit f6182ae

Please sign in to comment.