Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUM implementation in SwiftUI #339

Closed
AXR-Tech opened this issue Dec 10, 2020 · 13 comments
Closed

RUM implementation in SwiftUI #339

AXR-Tech opened this issue Dec 10, 2020 · 13 comments
Assignees

Comments

@AXR-Tech
Copy link

RUM implementation in SwiftUI

I have gone through the documentation doesn't mention anything regarding SwiftUI, natively. Is there a way to implement RUM in a native SwiftUI and not UIViewControllerRepresentable?

@ncreated
Copy link
Collaborator

Hey @Alexander-techIOS ! Thanks for reaching out 🙂. We plan to support SwiftUI in the future, but I can't give any estimates. Please contact our support so we can track your interest in having SwiftUI compatibility for the iOS RUM SDK 💪.

@ncreated ncreated self-assigned this Dec 11, 2020
@amkomatz-ggd
Copy link

+1

@saj11
Copy link

saj11 commented Mar 11, 2021

Hi @ncreated!
I don't sure if it is the same issue but I am trying to integrate Datadog into a SwiftUI app. I understand that Datadog do not support SwiftUI, in terms of RUM, so it won't track the views or actions. But RUM also includes a mode in which it can tracks the request, but it seems to do not work also.

So I want to know if any feature inside Datadog will work on a SwiftUI app

@ncreated
Copy link
Collaborator

Hello @saj11. Our manual instrumentation API offers a UI framework-agnostic variant which will work with SwiftUI as well. When using SwiftUI, you can leverage those two APIs to start and stop the RUM View:

Global.rum.startView(key:)
Global.rum.stopView(key:)

Network requests (auto)instrumentation will only work if there's a RUM View started, so please make sure that you call Global.rum.startView(key:) from your SwiftUI's view before the request is sent.

@hs-echkayben
Copy link

Hello @ncreated according to the manual instrumentation API you shared, Global.rum.startView(viewController: self) is the way to collect RUM resources. So what I don't understand how would that translate in the absence of a UIViewController in the case of swiftUI, the documentation is not clear when it comes to that. Thank you

@ncreated
Copy link
Collaborator

Hello @hs-echkayben 👋. In case of view controller absence, you can use following APIs on RUMMonitor:

/// Notifies that the View starts being presented to the user.
/// - Parameters:
/// - key: a `String` value identifying this View. It must match the `key` passed later to `stopView(key:attributes:)`.
/// - name: the View name used for RUM Explorer. If not provided, the `key` name will be used.
/// - attributes: custom attributes to attach to the View.
public func startView(
key: String,
name: String? = nil,
attributes: [AttributeKey: AttributeValue] = [:]
) {}
/// Notifies that the View stops being presented to the user.
/// - Parameters:
/// - key: a `String` value identifying this View. It must match the `key` passed earlier to `startView(key:path:attributes:)`.
/// - attributes: custom attributes to attach to the View.
public func stopView(
key: String,
attributes: [AttributeKey: AttributeValue] = [:]
) {}

With that, you can start and stop the view by giving String key to match its ending with the beginning. In case of SwiftUI, I think this should work fine:

struct ContentView: View {
    private let viewKey = "content-view" // must be unique
    private let rumViewName = "ContentView"

    var body: some View {
        // ...
        .onAppear {
           Global.rum.startView(key: viewKey, name: rumViewName)
        }
        .onDisappear {
           Global.rum.stopView(key: viewKey)
        }
    }
}

@maxep
Copy link
Member

maxep commented Feb 1, 2022

SwiftUI Instrumentation APIs have been added in 1.9.0. Our documentation still need to be updated but here some snippets on how to use it:

  • View Instrumentation:
import SwiftUI
import Datadog

struct FooView: View {

    var body: some View {
        FooContent {
            ...
        }
        .trackRUMView(name: "Foo")
    }
}
  • Tap Action Instrumentation:
import SwiftUI
import Datadog

struct BarView: View {

    var body: some View {
        Button("BarButton") { {
            ...
        }
        .trackRUMTapAction(name: "Bar")
    }
}

It has been designed to work seamlessly with UIKit auto-instrumentation for hybrid UIKit+SwiftUI applications.
I will close this issue, but feel free to add any comment/feedback.

@maxep maxep closed this as completed Feb 1, 2022
@ferologics
Copy link

@maxep thanks for the update, this is very helpful! I'm currently integrating I'm curious how does the trackRUMView(name:) method behave in case onDisappear() of the view is not called?

@maxep
Copy link
Member

maxep commented Mar 4, 2022

Hey @ferologics! Thanks for using it :)

We keep a stack of appeared view to consistently log start and stop view events, the last item of the stack is the visible one, any items below it have appeared but onDisappear has not been called. Therefore, they are considered not visible but can be revealed if the last item disappears.

So if we have the following sequence, characteristically of a modal navigation:

  1. View A onAppear
  2. View B onAppear
  3. View B onDisappear

The following events will be logged:

  1. Start View A
  2. Stop View A
  3. Start View B
  4. Stop View B
  5. Start View A

@payal10gupta
Copy link

payal10gupta commented Apr 10, 2024

Hey @maxep Does the above mentioned apis work with the native SwiftUI apps?

@maxep
Copy link
Member

maxep commented Apr 10, 2024

Hi @payal10gupta,

You mean apps fully written using SwiftUI? Yes it will, on iOS and tvOS platforms.

@payal10gupta
Copy link

payal10gupta commented Apr 10, 2024

Hey @maxep ,

Thanks for responding.

So far I am able to report the crashes and upload the dSYMs on the portal. There are a few issues that I am facing,

  1. When symbolicating the crashes, it is not showing the exact file/class names.
  2. I am trying the Mobile Session Replay feature of Datadog. The recoded sessions does not show anything. Just a SwiftUI label on the entire screen

Here is how I'm initializing the datadog,

Datadog.initialize(
    with: Datadog.Configuration(
        clientToken: clientToken,
        env: environment,
        service: serviceName
    ),
    trackingConsent: .granted
)

RUM.enable(
    with: RUM.Configuration(
        applicationID: appID,
        uiKitViewsPredicate: DefaultUIKitRUMViewsPredicate(),
        uiKitActionsPredicate: DefaultUIKitRUMActionsPredicate()
    )
)

CrashReporting.enable()

And using the below code to track the SwiftUI views,

.trackRUMView(name: viewName)

Am I missing something here?

@maxep
Copy link
Member

maxep commented Apr 10, 2024

Hi @payal10gupta,

This GH issue is about SwiftUI support in RUM.
In regards to Crash Reporting, I invite you to contact our support, you will be able to share more privately about your configuration and they can assist you with symbolication.

Session Replay does not yet support SwiftUI, it is on our roadmap so stay tuned!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants