firebase-ios-sdk icon indicating copy to clipboard operation
firebase-ios-sdk copied to clipboard

[Swift Concurrency] Performance Monitoring does not collect network traces when using URLSession's `async` methods

Open moyerr opened this issue 1 year ago • 9 comments

Description

I noticed that my app's network requests were not showing up at all in Performance Monitoring. I traced the issue to the fact that I am using URLSession's concurrency (async/await) APIs.

I can tell everything is set up properly because I can see the network traces when I use URLSession's closure-based APIs.

Reproducing the issue

// ❌ This network call does not show up in Performance Monitoring
let (data, response) = try await URLSession.shared.data(for: request)

// ✅ This network call does show up in Performance monitoring
URLSession.shared
  .dataTask(with: try request) { data, response, error in
    // Handle the response
  }
  .resume()

Firebase SDK Version

10.15.0

Xcode Version

14.3.1

Installation Method

Swift Package Manager

Firebase Product(s)

Performance

Targeted Platforms

iOS

Relevant Log Output

No response

If using Swift Package Manager, the project's Package.resolved

No response

If using CocoaPods, the project's Podfile.lock

No response

moyerr avatar Sep 28 '23 02:09 moyerr

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Sep 28 '23 02:09 google-oss-bot

Hi @moyerr, I tried to reproduce the issue using the code snippet you shared, but I'm able to see my network requests in the console. Could you provide an MCVE or complete code snippet that reproduces the issue?

rizafran avatar Sep 29 '23 16:09 rizafran

Hey @moyerr. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Oct 06 '23 01:10 google-oss-bot

Since there haven't been any recent updates here, I am going to close this issue.

@moyerr if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

google-oss-bot avatar Oct 13 '23 01:10 google-oss-bot

I am seeing the same issue, but it appears to be iOS version specific.

  • On iOS 16 and 17, network requests that are made via the async URLSession data(for request) function are not recorded and do not appear in the logs when -FIRDebugEnabled is used.
  • public func data(for request: URLRequest) async throws -> (Data, URLResponse)
  • On iOS 15, it works as expected and the following is reported. 10.16.0 - [FirebasePerformance][I-PRF100009] Logging network request trace - {url}, Response code: 200, 928.3030ms

I am able to see logs for requests made using the completionHandler based URLSession functions across all iOS versions without any issues

  • public func dataTask(with request: URLRequest, completionHandler: @escaping CompletionHandler) -> URLSessionDataTask

Firebase version:

  • Firebase 10.16.0

Xcode version:

  • Xcode 15.0

roger-smith avatar Oct 18 '23 16:10 roger-smith

Hey I was just checking out some things in the pics I took last night of someone’s phone. Can you guys tell me what’s going on ? If it’s what I’m thinking I need confirmation that I’m either wrong or right.BrooksieOn Oct 18, 2023, at 12:57 PM, Morgan Chen @.***> wrote: Reopened #11861.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.***>

Brooksie18 avatar Oct 18 '23 17:10 Brooksie18

BrooksieOn Oct 18, 2023, at 1:09 PM, Rachel McBride @.> wrote:Hey I was just checking out some things in the pics I took last night of someone’s phone. Can you guys tell me what’s going on ? If it’s what I’m thinking I need confirmation that I’m either wrong or right.BrooksieOn Oct 18, 2023, at 12:57 PM, Morgan Chen @.> wrote: Reopened #11861.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.***>

Brooksie18 avatar Oct 20 '23 15:10 Brooksie18

Is it possible that the method swizzling isn't attaching to the async variant of URLSession and thus it isn't instrumenting it?

You could write an extension on URLSession that wrappers the async call and creates an HTTPMetric. Automatically start before making the call, and stop on response or throws. To prevent double reporting disable the instrumentation. The downside of this is you don't get the app lifecycle traces then--such as _app_start. Presently that 100% internal and a developer cannot replicate it because of lack of public initializers on the FIRTrace and inability to set custom start/stop times.

I don't want any method swizzling going on for this exact kind of reason and want to be 100% in control of the network traces. https://github.com/firebase/firebase-ios-sdk/issues/12177

SwiftNativeDeveloper avatar Jan 16 '24 21:01 SwiftNativeDeveloper