Hey guys! I’m working on porting an existing iOS a...
# ktor
c
Hey guys! I’m working on porting an existing iOS app to Kotlin Multiplatform Mobile and I’m struggling in figuring out how to integrate Datadog with Ktor. I see Datadog provides a Java library, that cannot be used in the shared module of my app. I then thought of passing to the shared kotlin code an instance of DDURLSessionDelegate, which is the delegate responsible for tracing automatically every call performed through an URLSession on iOS. But unfortunately, with Ktor 2.2.1 is not possible anymore to pass a custom session with a custom delegate as a preconfigured Darwin session. The only doable thing is to create instance of KtorNSURLSessionDelegate, that is a completely sealed class. Do you have any suggestion? I’ll give more detail if needed :) Any help will be really appreciated! Thanks
r
What do you mean by
KtorNSURLSessionDelegate
is a sealed class? You should create custom
NsUrlSessionDelegate
and call both Datadog and Ktor delegates from it. You can see in the API docs which methods are required to call.
c
Sorry Rustam, I used the wrong terminology. I mean that KtorNSURLSessionDelegate has an internal constructor I cannot directly call. I cannot subclass KtorNSURLSessionDelegate directly.
r
You can use factory function
KtorNSURLSessionDelegate
to create an instance of it
c
How do I subclass it, though? The constructor is internal. I need to subclass it. My aim is to decorate the datadog delegate and pass it to the darwin engine stack. How can I achieve it?
Would something like this make sense?
Copy code
class CustomDelegate(private val datadogDelegate: NSURLSessionDelegateProtocol, private val ktorDelegate: KtorNSURLSessionDelegate): NSObject(), NSURLSessionDelegateProtocol {

    override fun URLSession(session: NSURLSession, didBecomeInvalidWithError: NSError?) {
        datadogDelegate.URLSession(session, didBecomeInvalidWithError)
        ktorDelegate.URLSession(session, didBecomeInvalidWithError)
    }
}

fun test(datadogDelegate: NSURLSessionDelegateProtocol) {
    val client = HttpClient(Darwin) {
        engine {

            val ktorDelegate = KtorNSURLSessionDelegate()
            val session = NSURLSession.sessionWithConfiguration(NSURLSessionConfiguration.defaultSessionConfiguration, CustomDelegate(datadogDelegate, ktorDelegate), null)

            usePreconfiguredSession(session, ktorDelegate)
        }
    }
}
r
Yes, this is exactly what you need to do. Please note, that these methods are required to call from your delegate
Copy code
* For HTTP requests to work property, it's important that users call these functions:
 *   * URLSession:dataTask:didReceiveData:
 *   * URLSession:task:didCompleteWithError:
 *   * URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:
 *
 * For WebSockets to work, it's important that users call these functions:
 *   * URLSession:webSocketTask:didOpenWithProtocol:
 *   * URLSession:webSocketTask:didCloseWithCode:reason:
c
Ok, thanks! So, what’s the purpose of KtorNSURLSessionDelegate then?
r
It bridges Ktor and NSURLSession
c
So, it provides some internal logic needed by Ktor to work properly. Many hanks @Rustam Siniukov!
👍 1
c
@Calogero where you able to make this work? I’ve been trying to do a similar thing but no luck. @Rustam Siniukov when you say it is important to call those functions. Is it in the kotlin side where the CustomDelegate is? Or in the delegate that lives in the native side?
c
Hi @Carlos Monzon. I was able to let it work 🙂 . I'll post the detail here tomorrow. Just saw your message. 🙂
🙏 1
c
Thanks Calogero. Looking forward to it
c
Sorry for the delay... I've been working very hard on a project and I didn't have time to post here Here is a repository where you can find a sample integration https://github.com/csanfilippo/datadog-ktor-test
c
This is awesome @Calogero Definitely worth sharing a blog about it! I end up doing a similar approach as yours but I created the ktorDelegate on the iOS side, but in your case is created in the shared side which is much cleaner 👍
c
My delegate lives in the iOS side. It cannot stay in the common code, because Ktor+Darwin has its peculiarities
@Carlos Monzon, consider that my example works with Datadog 1.x. I've never migrated my code to Datadog 2.x so far.
150 Views