Skip to content

Commit a498e1e

Browse files
authored
Merge pull request #2350 from DataDog/gonzalezreal/RUM-10418/swiftui-sr-privacy-benchmark-tests
Add SwiftUI privacy overrides to benchmark tests
2 parents 4163546 + 46c64af commit a498e1e

File tree

7 files changed

+248
-85
lines changed

7 files changed

+248
-85
lines changed

BenchmarkTests/CatalogSwiftUI/ContentView.swift

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ public struct ContentView: View {
4040
startRadiusFraction: 0.3,
4141
endRadiusFraction: 5)
4242

43-
private let monitor: DatadogMonitor
44-
45-
public init(monitor: DatadogMonitor) {
46-
self.monitor = monitor
47-
43+
public init() {
4844
// change the background of all the tables
4945
UITableView.appearance().backgroundColor = .clear
5046
}
@@ -70,7 +66,7 @@ public struct ContentView: View {
7066
}
7167
.scrollContentBackground(.hidden)
7268
.navigationTitle("SwiftUI Catalog")
73-
.modifier(monitor.viewModifier(name: "SwiftUI Catalog"))
69+
.trackView(name: "SwiftUI Catalog")
7470
}
7571

7672
}
@@ -81,13 +77,15 @@ public struct ContentView: View {
8177

8278
private var topHeaderRow: some View {
8379
Group {
84-
Text("A catalog of components, controls, effects, styles and accessibility elements you can use to develop SwiftUI Interfaces in iOS and iPadOS.")
85-
.font(.footnote)
86-
.fontWeight(.light)
87-
.font(.title2)
88-
.padding(.top, 24)
89-
.padding(.bottom, 16)
90-
.foregroundColor(.white)
80+
PrivacyView(text: .maskAll) {
81+
Text("A catalog of components, controls, effects, styles and accessibility elements you can use to develop SwiftUI Interfaces in iOS and iPadOS.")
82+
.font(.footnote)
83+
.fontWeight(.light)
84+
.font(.title2)
85+
.padding(.top, 24)
86+
.padding(.bottom, 16)
87+
.foregroundColor(.white)
88+
}
9189

9290
HStack(alignment: .center, spacing: 2) {
9391
Spacer()
@@ -114,31 +112,31 @@ public struct ContentView: View {
114112
.font(.title)
115113
.modifier(ListSectionFontModifier())) {
116114
Group {
117-
Link(destination: ButtonsComponentsView().modifier(monitor.viewModifier(name: "Buttons")),
115+
Link(destination: ButtonsComponentsView().trackView(name: "Buttons"),
118116
label: "Buttons")
119-
Link(destination: ImagesComponentView().modifier(monitor.viewModifier(name: "Images")),
117+
Link(destination: ImagesComponentView().trackView(name: "Images"),
120118
label: "Images")
121-
Link(destination: TextsComponentsView().modifier(monitor.viewModifier(name: "Texts")),
119+
Link(destination: TextsComponentsView().trackView(name: "Texts"),
122120
label: "Texts")
123-
Link(destination: LabelsView().modifier(monitor.viewModifier(name: "Labels")),
121+
Link(destination: LabelsView().trackView(name: "Labels"),
124122
label: "Labels")
125-
Link(destination: MenusComponentView().modifier(monitor.viewModifier(name: "Menus")),
123+
Link(destination: MenusComponentView().trackView(name: "Menus"),
126124
label: "Menus")
127125
}
128126
Group {
129-
Link(destination: TogglesView().modifier(monitor.viewModifier(name: "Toggles")),
127+
Link(destination: TogglesView().trackView(name: "Toggles"),
130128
label: "Toggles")
131-
Link(destination: SlidersView().modifier(monitor.viewModifier(name: "Sliders")),
129+
Link(destination: SlidersView().trackView(name: "Sliders"),
132130
label: "Sliders")
133-
Link(destination: SteppersView().modifier(monitor.viewModifier(name: "Steppers")),
131+
Link(destination: SteppersView().trackView(name: "Steppers"),
134132
label: "Steppers")
135-
Link(destination: PickersView().modifier(monitor.viewModifier(name: "Pickers")),
133+
Link(destination: PickersView().trackView(name: "Pickers"),
136134
label: "Pickers")
137-
Link(destination: DatePickersView().modifier(monitor.viewModifier(name: "Date Pickers")),
135+
Link(destination: DatePickersView().trackView(name: "Date Pickers"),
138136
label: "Date Pickers")
139-
Link(destination: ColorPickersView().modifier(monitor.viewModifier(name: "Color Pickers")),
137+
Link(destination: ColorPickersView().trackView(name: "Color Pickers"),
140138
label: "Color Pickers")
141-
Link(destination: ProgressViews().modifier(monitor.viewModifier(name: "Progress View")),
139+
Link(destination: ProgressViews().trackView(name: "Progress View"),
142140
label: "Progress View")
143141
}
144142
}
@@ -151,7 +149,7 @@ public struct ContentView: View {
151149
.modifier(ListSectionFontModifier())) {
152150
Group {
153151
Link(destination: ExampleProductView(productId: "product.consumable.example.1", productImageName: "giftcard.fill"),
154-
label: "Consumable Product View").modifier(monitor.viewModifier(name: "Consumable Product View"))
152+
label: "Consumable Product View").trackView(name: "Consumable Product View")
155153
}
156154
}
157155
.listRowBackground(Color(sectionColor, bundle: .module))
@@ -162,17 +160,17 @@ public struct ContentView: View {
162160
Section(header: Text("Layouts")
163161
.font(.title)
164162
.modifier(ListSectionFontModifier())) {
165-
Link(destination: ListsComponentView().modifier(monitor.viewModifier(name: "Lists")),
163+
Link(destination: ListsComponentView().trackView(name: "Lists"),
166164
label: "Lists")
167-
Link(destination: StacksView().modifier(monitor.viewModifier(name: "Stacks")),
165+
Link(destination: StacksView().trackView(name: "Stacks"),
168166
label: "Stacks")
169-
Link(destination: GridsView().modifier(monitor.viewModifier(name: "Grids")),
167+
Link(destination: GridsView().trackView(name: "Grids"),
170168
label: "Grids")
171-
Link(destination: ContainersView().modifier(monitor.viewModifier(name: "Containers")),
169+
Link(destination: ContainersView().trackView(name: "Containers"),
172170
label: "Containers")
173-
Link(destination: ScrollViewsView().modifier(monitor.viewModifier(name: "Scrollviews")),
171+
Link(destination: ScrollViewsView().trackView(name: "Scrollviews"),
174172
label: "Scrollviews")
175-
Link(destination: TableViews().modifier(monitor.viewModifier(name: "Table Views")),
173+
Link(destination: TableViews().trackView(name: "Table Views"),
176174
label: "Table Views")
177175
}
178176
.listRowBackground(Color(sectionColor, bundle: .module))
@@ -181,13 +179,13 @@ public struct ContentView: View {
181179
var hierachicalViews: some View {
182180
Section(header: Text("Hierachical Views") .font(.title)
183181
.modifier(ListSectionFontModifier())) {
184-
Link(destination: NavigationBarsComponentView().modifier(monitor.viewModifier(name: "Navigation")),
182+
Link(destination: NavigationBarsComponentView().trackView(name: "Navigation"),
185183
label: "Navigation")
186-
Link(destination: OutlinesGroupsView().modifier(monitor.viewModifier(name: "Outlines")),
184+
Link(destination: OutlinesGroupsView().trackView(name: "Outlines"),
187185
label: "Outlines")
188-
Link(destination: DisclosureGroupsView().modifier(monitor.viewModifier(name: "Disclosures")),
186+
Link(destination: DisclosureGroupsView().trackView(name: "Disclosures"),
189187
label: "Disclosures")
190-
Link(destination: TabsView().modifier(monitor.viewModifier(name: "Tabs")),
188+
Link(destination: TabsView().trackView(name: "Tabs"),
191189
label: "Tabs")
192190
}
193191
.listRowBackground(Color(sectionColor, bundle: .module))
@@ -198,15 +196,15 @@ public struct ContentView: View {
198196
Section(header: Text("Drawing and animations")
199197
.font(.title)
200198
.modifier(ListSectionFontModifier())) {
201-
Link(destination: CanvasView().modifier(monitor.viewModifier(name: "Canvas")),
199+
Link(destination: CanvasView().trackView(name: "Canvas"),
202200
label: "Canvas")
203-
Link(destination: GraphicContextsView().modifier(monitor.viewModifier(name: "Graphic Context")),
201+
Link(destination: GraphicContextsView().trackView(name: "Graphic Context"),
204202
label: "Graphic Context")
205-
Link(destination: ShapesView().modifier(monitor.viewModifier(name: "Shapes")),
203+
Link(destination: ShapesView().trackView(name: "Shapes"),
206204
label: "Shapes")
207-
Link(destination: AnimationsView().modifier(monitor.viewModifier(name: "Animations")),
205+
Link(destination: AnimationsView().trackView(name: "Animations"),
208206
label: "Animations")
209-
Link(destination: GeometriesView().modifier(monitor.viewModifier(name: "Geometries")),
207+
Link(destination: GeometriesView().trackView(name: "Geometries"),
210208
label: "Geometries")
211209
}
212210
.listRowBackground(Color(sectionColor, bundle: .module))
@@ -218,7 +216,7 @@ public struct ContentView: View {
218216
Section(header: Text("Charts")
219217
.font(.title)
220218
.modifier(ListSectionFontModifier())) {
221-
Link(destination: ChartsViews().modifier(monitor.viewModifier(name: "Swift Charts")),
219+
Link(destination: ChartsViews().trackView(name: "Swift Charts"),
222220
label: "Swift Charts")
223221
}
224222
.listRowBackground(Color(sectionColor, bundle: .module))
@@ -229,11 +227,11 @@ public struct ContentView: View {
229227
Section(header: Text("Gestures")
230228
.font(.title)
231229
.modifier(ListSectionFontModifier())) {
232-
Link(destination: GesturesView().modifier(monitor.viewModifier(name: "Gestures")),
230+
Link(destination: GesturesView().trackView(name: "Gestures"),
233231
label: "Gestures")
234-
Link(destination: ComposingGesturesView().modifier(monitor.viewModifier(name: "Composing Gestures")),
232+
Link(destination: ComposingGesturesView().trackView(name: "Composing Gestures"),
235233
label: "Composing Gestures")
236-
Link(destination: SensoryFeedbackInViews().modifier(monitor.viewModifier(name: "Sensory Feedback")),
234+
Link(destination: SensoryFeedbackInViews().trackView(name: "Sensory Feedback"),
237235
label: "Sensory Feedback")
238236
}
239237
.listRowBackground(Color(sectionColor, bundle: .module))
@@ -244,11 +242,11 @@ public struct ContentView: View {
244242
Section(header: Text("View modifiers")
245243
.font(.title)
246244
.modifier(ListSectionFontModifier())) {
247-
Link(destination: TextModifiersView().modifier(monitor.viewModifier(name: "Text modifiers")),
245+
Link(destination: TextModifiersView().trackView(name: "Text modifiers"),
248246
label: "Text modifiers")
249-
Link(destination: EffectsModifiersView().modifier(monitor.viewModifier(name: "Effect modifiers")),
247+
Link(destination: EffectsModifiersView().trackView(name: "Effect modifiers"),
250248
label: "Effect modifiers")
251-
Link(destination: LayoutModifiersView().modifier(monitor.viewModifier(name: "Layout modifiers")),
249+
Link(destination: LayoutModifiersView().trackView(name: "Layout modifiers"),
252250
label: "Layout modifiers")
253251

254252
}
@@ -261,7 +259,7 @@ public struct ContentView: View {
261259
.font(.title)
262260
.modifier(ListSectionFontModifier())) {
263261

264-
Link(destination: AccesibilityView().modifier(monitor.viewModifier(name: "Accesibility")),
262+
Link(destination: AccesibilityView().trackView(name: "Accesibility"),
265263
label: "Accesibility")
266264
}
267265
.listRowBackground(Color(sectionColor, bundle: .module))
@@ -272,7 +270,7 @@ public struct ContentView: View {
272270
Section(header: Text("Status and tool bars")
273271
.font(.title)
274272
.modifier(ListSectionFontModifier())) {
275-
Link(destination: ToolbarsComponentView().modifier(monitor.viewModifier(name: "Tool Bars")),
273+
Link(destination: ToolbarsComponentView().trackView(name: "Tool Bars"),
276274
label: "Tool Bars")
277275
}
278276
.listRowBackground(Color(sectionColor, bundle: .module))
@@ -284,7 +282,7 @@ public struct ContentView: View {
284282
.font(.title)
285283
.modifier(ListSectionFontModifier())) {
286284

287-
Link(destination: StylesView().modifier(monitor.viewModifier(name: "Styles")),
285+
Link(destination: StylesView().trackView(name: "Styles"),
288286
label: "Styles")
289287
}
290288
.listRowBackground(Color(sectionColor, bundle: .module))
@@ -298,7 +296,7 @@ public struct ContentView: View {
298296

299297
// Link(destination: PopoversComponentView(),
300298
// label: "Popovers")
301-
Link(destination: SheetsView().modifier(monitor.viewModifier(name: "Sheets")),
299+
Link(destination: SheetsView().trackView(name: "Sheets"),
302300
label: "Sheets")
303301
// Link(destination: AlertsComponentView(),
304302
// label: "Alerts")
@@ -315,13 +313,13 @@ public struct ContentView: View {
315313
Section(header: Text("Composed components to help speed up development")
316314
.font(.title)
317315
.modifier(ListSectionFontModifier())) {
318-
Link(destination: CommonlyUsedViews().modifier(monitor.viewModifier(name: "Commonly used views")),
316+
Link(destination: CommonlyUsedViews().trackView(name: "Commonly used views"),
319317
label: "Commonly used views")
320-
Link(destination: CollectionsViews().modifier(monitor.viewModifier(name: "Collections of components")),
318+
Link(destination: CollectionsViews().trackView(name: "Collections of components"),
321319
label: "Collections of components")
322-
Link(destination: StackedCardsView<CardView>(elementsCount: 22).modifier(monitor.viewModifier(name: "Stacked cards with dragging")),
320+
Link(destination: StackedCardsView<CardView>(elementsCount: 22).trackView(name: "Stacked cards with dragging"),
323321
label: "Stacked cards with dragging")
324-
Link(destination: InterfacingWithUIKitView(pages: ModelData().features.map { FeatureCardView(landmark: $0) }).modifier(monitor.viewModifier(name: "UIKit Interface")),
322+
Link(destination: InterfacingWithUIKitView(pages: ModelData().features.map { FeatureCardView(landmark: $0) }).trackView(name: "UIKit Interface"),
325323
label: "UIKit Interface")
326324
}
327325
.listRowBackground(Color(sectionColor, bundle: .module))
@@ -337,7 +335,7 @@ public struct ContentView: View {
337335
#Preview {
338336

339337
Group {
340-
ContentView(monitor: NOPDatadogMonitor())
338+
ContentView()
341339
.preferredColorScheme(.dark)
342340
}
343341
}

BenchmarkTests/CatalogSwiftUI/DatadogMonitor.swift

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,99 @@
66

77
import SwiftUI
88

9+
@frozen public enum TextPrivacyLevel {
10+
case maskSensitiveInputs
11+
case maskAllInputs
12+
case maskAll
13+
}
14+
15+
@frozen public enum ImagePrivacyLevel {
16+
case maskNonBundledOnly
17+
case maskAll
18+
case maskNone
19+
}
20+
21+
@frozen public enum TouchPrivacyLevel {
22+
case show
23+
case hide
24+
}
25+
926
public protocol DatadogMonitor {
1027
func viewModifier(name: String) -> AnyViewModifier
1128
func actionModifier(name: String) -> AnyViewModifier
29+
func privacyView<Content: View>(
30+
text: TextPrivacyLevel?,
31+
image: ImagePrivacyLevel?,
32+
touch: TouchPrivacyLevel?,
33+
hide: Bool?,
34+
@ViewBuilder content: @escaping () -> Content
35+
) -> AnyView
1236
}
1337

1438
struct NOPDatadogMonitor: DatadogMonitor {
1539
func viewModifier(name: String) -> AnyViewModifier { AnyViewModifier() }
1640
func actionModifier(name: String) -> AnyViewModifier { AnyViewModifier() }
41+
func privacyView<Content: View>(
42+
text: TextPrivacyLevel?,
43+
image: ImagePrivacyLevel?,
44+
touch: TouchPrivacyLevel?,
45+
hide: Bool?,
46+
content: @escaping () -> Content
47+
) -> AnyView {
48+
AnyView(content())
49+
}
50+
}
51+
52+
extension EnvironmentValues {
53+
@Entry public var datadogMonitor: any DatadogMonitor = NOPDatadogMonitor()
54+
}
55+
56+
struct PrivacyView<Content: View>: View {
57+
@Environment(\.datadogMonitor) private var monitor
58+
59+
private var text: TextPrivacyLevel?
60+
private var image: ImagePrivacyLevel?
61+
private var touch: TouchPrivacyLevel?
62+
private var hide: Bool?
63+
private let content: () -> Content
64+
65+
init(
66+
text: TextPrivacyLevel? = nil,
67+
image: ImagePrivacyLevel? = nil,
68+
touch: TouchPrivacyLevel? = nil,
69+
hide: Bool? = nil,
70+
@ViewBuilder content: @escaping () -> Content
71+
) {
72+
self.text = text
73+
self.image = image
74+
self.touch = touch
75+
self.hide = hide
76+
self.content = content
77+
}
78+
79+
var body: some View {
80+
monitor.privacyView(
81+
text: text,
82+
image: image,
83+
touch: touch,
84+
hide: hide,
85+
content: content
86+
)
87+
}
88+
}
89+
90+
extension View {
91+
func trackView(name: String) -> some View {
92+
modifier(TrackViewModifier(name: name))
93+
}
94+
}
95+
96+
private struct TrackViewModifier: ViewModifier {
97+
@Environment(\.datadogMonitor) private var monitor
98+
99+
var name: String
100+
101+
func body(content: Content) -> some View {
102+
content.modifier(monitor.viewModifier(name: name))
103+
}
17104
}

0 commit comments

Comments
 (0)