We have product for network monitoring and we are't able to add support auto-instrumenting the networking requests for URLSession async/wait methods as these methods are't exposed to dynamic environment or not exposed to ObjC and we con't use any of the run-time functionality and we con't override these methods as these methods are't public.
looking for a way to add some kind of logic so that when customers use our product they don't have to add any code from there end to monitor this system.
There isn’t a good way to achieve this goal.
I’ve seen folks attempt to do this in a variety of ways, and none of them end well. Specifically:
-
Swizzling — DTS doesn’t support folks swizzling methods on Apple classes because it’s not a sustainable path. Eventually something comes along and breaks you. It sounds like you’ve hit that case with these Swift async methods.
-
URLProtocol
— Implementing a recursiveURLProtocol
is super hard to do correctly [1], and it comes with a bunch of feature limitations and compatibility risk. Moreover, if code within the process is using a customURLSession
, then it has to opt in anyway, and if you require an opt in then you might as well require a nice opt in (see below).
As to what you should do right now, it kinda depends on the context. If you only need to help folks at development time, I’m a big fan of the HTTP instrument, per Analyzing HTTP traffic with Instruments. Keep in mind that Instruments has all sorts of automation options, most notably the xctrace
tool.
However, most folks want to do this when the app is deployed, and for understandable reasons. In that case my advice is that you have your clients opt in to this support. A good way for you to do that is for you to provide a hook for them to deliver URLSessionTaskMetrics
values to you.
I understand that forcing clients to opt in is less than ideal, but right now it’s the only compatible solution. If you’d like to see that change, you should file an enhancement request for the features you need.
Actually, I can imagine two possible ERs here:
-
You could request an explicit mechanism for
URLSession
to ‘broadcast’ metrics within the process. -
You could request support for what you need from MetricKit.
Or you could do both (-:
If you do file any ERs, please post your bug numbers, just for the record.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[1] I wrote the ‘book’ on this, in the CustomHTTPProtocol sample code read me, and… yeah… it’s not fun.