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-1660 chore: Enhance RUM session debugging in Example app #1902

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 44 additions & 16 deletions Datadog/Example/Debugging/DebugRUMSessionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ private class DebugRUMSessionViewModel: ObservableObject {
var id: UUID = UUID()
}

@Published var sessionItems: [SessionItem] = []
@Published var sessionItems: [SessionItem] = [] {
didSet { updateSessionID() }
}
@Published var sessionID: String = ""

@Published var viewKey: String = ""
@Published var actionName: String = ""
Expand All @@ -46,12 +49,18 @@ private class DebugRUMSessionViewModel: ObservableObject {

var urlSessions: [URLSession] = []

init() {
updateSessionID()
}

func startView() {
guard !viewKey.isEmpty else {
return
}

let key = viewKey
RUMMonitor.shared().startView(key: key)

sessionItems.append(
SessionItem(
label: key,
Expand All @@ -67,7 +76,6 @@ private class DebugRUMSessionViewModel: ObservableObject {
)
)

RUMMonitor.shared().startView(key: key)
self.viewKey = ""
}

Expand All @@ -76,11 +84,11 @@ private class DebugRUMSessionViewModel: ObservableObject {
return
}

RUMMonitor.shared().addAction(type: .custom, name: actionName)
sessionItems.append(
SessionItem(label: actionName, type: .action, isPending: false, stopAction: nil)
)

RUMMonitor.shared().addAction(type: .custom, name: actionName)
self.actionName = ""
}

Expand All @@ -89,11 +97,11 @@ private class DebugRUMSessionViewModel: ObservableObject {
return
}

RUMMonitor.shared().addError(message: errorMessage)
sessionItems.append(
SessionItem(label: errorMessage, type: .error, isPending: false, stopAction: nil)
)

RUMMonitor.shared().addError(message: errorMessage)
self.errorMessage = ""
}

Expand All @@ -103,6 +111,7 @@ private class DebugRUMSessionViewModel: ObservableObject {
}

let key = self.resourceKey
RUMMonitor.shared().startResource(resourceKey: key, url: mockURL())
sessionItems.append(
SessionItem(
label: key,
Expand All @@ -118,7 +127,6 @@ private class DebugRUMSessionViewModel: ObservableObject {
)
)

RUMMonitor.shared().startResource(resourceKey: key, url: mockURL())
self.resourceKey = ""
}

Expand Down Expand Up @@ -161,6 +169,11 @@ private class DebugRUMSessionViewModel: ObservableObject {
urlSessions.append(session) // keep session
}

func stopSession() {
RUMMonitor.shared().stopSession()
sessionItems = []
}

// MARK: - Private

private func modifySessionItem(type: SessionItemType, label: String, change: (inout SessionItem) -> Void) {
Expand All @@ -176,6 +189,14 @@ private class DebugRUMSessionViewModel: ObservableObject {
private func mockURL() -> URL {
return URL(string: "https://foo.com/\(UUID().uuidString)")!
}

private func updateSessionID() {
RUMMonitor.shared().currentSessionID { [weak self] id in
DispatchQueue.main.async {
self?.sessionID = id ?? "-"
}
}
}
}

@available(iOS 13.0, *)
Expand Down Expand Up @@ -215,6 +236,10 @@ internal struct DebugRUMSessionView: View {
)
Button("START") { viewModel.startResource() }
}
HStack {
Button("STOP SESSION") { viewModel.stopSession() }
Spacer()
}
Divider()
}
Group {
Expand Down Expand Up @@ -248,9 +273,12 @@ internal struct DebugRUMSessionView: View {
Divider()
}
Group {
Text("Current RUM Session:")
Text("Current RUM Session")
.frame(maxWidth: .infinity, alignment: .leading)
.font(.caption.weight(.bold))
Text("UUID: \(viewModel.sessionID)")
.frame(maxWidth: .infinity, alignment: .leading)
.font(.caption.weight(.ultraLight))
List(viewModel.sessionItems) { sessionItem in
SessionItemView(item: sessionItem)
.listRowInsets(EdgeInsets())
Expand All @@ -277,20 +305,20 @@ private struct FormItemView: View {
Text(title)
.bold()
.font(.system(size: 10))
.padding(8)
.padding(4)
.background(accent)
.foregroundColor(Color.white)
.cornerRadius(8)
.cornerRadius(4)
TextField(placeholder, text: $value)
.font(.system(size: 12))
.padding(8)
.padding(4)
.background(Color(UIColor.secondarySystemFill))
.cornerRadius(8)
.cornerRadius(4)
}
.padding(8)
.padding(4)
.background(Color(UIColor.systemFill))
.foregroundColor(Color.secondary)
.cornerRadius(8)
.cornerRadius(4)
}
}

Expand All @@ -304,20 +332,20 @@ private struct SessionItemView: View {
Text(label(for: item.type))
.bold()
.font(.system(size: 10))
.padding(8)
.padding(4)
.background(color(for: item.type))
.foregroundColor(Color.white)
.cornerRadius(8)
.cornerRadius(4)
Text(item.label)
.bold()
.font(.system(size: 14))
Spacer()
}
.padding(8)
.padding(4)
.frame(maxWidth: .infinity)
.background(Color(UIColor.systemFill))
.foregroundColor(Color.secondary)
.cornerRadius(8)
.cornerRadius(4)

if item.isPending {
Button("STOP") { item.stopAction?() }
Expand Down
6 changes: 3 additions & 3 deletions Datadog/Example/Debugging/Helpers/SwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ extension Color {
internal struct DatadogButtonStyle: ButtonStyle {
func makeBody(configuration: DatadogButtonStyle.Configuration) -> some View {
return configuration.label
.font(.system(size: 14, weight: .medium))
.padding(10)
.font(.system(size: 12, weight: .medium))
.padding(6)
.background(Color.datadogPurple)
.foregroundColor(.white)
.cornerRadius(8)
.cornerRadius(6)
}
}