Skip to content

Commit

Permalink
track path separately from screen.name
Browse files Browse the repository at this point in the history
  • Loading branch information
MustafaHaddara committed Dec 10, 2024
1 parent 87d153f commit 6dd5460
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ UIKit views will automatically be instrumented, emitting `viewDidAppear` and `vi
- Storybook Identifier - unique id identifying the view controller within its Storybook
- `view.title` - as defined above
- `view.class` - as defined above
- `screen.path` - the full path leading to the current view, consisting of the current view's `screen.name` as well as any parent views.

`viewDidAppear` events will also track `screen.name` as the "current screen" (as with the manual instrumentation described below), and will include that value as `screen.name` on other, non-navigation spans emitted.

Expand Down
23 changes: 18 additions & 5 deletions Sources/Honeycomb/HoneycombNavigationSpanInstrumentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func getTracer() -> Tracer {

internal class HoneycombNavigationProcessor {
static let shared = HoneycombNavigationProcessor()
var currentNavigationPath: String? = nil
var currentNavigationPath: [String] = []

private init() {}

Expand All @@ -29,7 +29,7 @@ internal class HoneycombNavigationProcessor {
}

func reportNavigation(path: String) {
currentNavigationPath = path
currentNavigationPath = [path]

// emit a span that says we've navigated to this path
getTracer().spanBuilder(spanName: navigationSpanName)
Expand Down Expand Up @@ -71,7 +71,7 @@ internal class HoneycombNavigationProcessor {
reportNavigation(path: unencodablePath)
}

func setCurrentNavigationPath(_ path: String) {
func setCurrentNavigationPath(_ path: [String]) {
currentNavigationPath = path
}
}
Expand Down Expand Up @@ -99,13 +99,26 @@ public struct HoneycombNavigationPathSpanProcessor: SpanProcessor {
parentContext: SpanContext?,
span: any ReadableSpan
) {
if let currentPath = HoneycombNavigationProcessor.shared.currentNavigationPath {
let currentViewPath = HoneycombNavigationProcessor.shared.currentNavigationPath
if !currentViewPath.isEmpty {
span.setAttribute(
key: "screen.name",
value: currentPath
value: currentViewPath.last!
)
span.setAttribute(
key: "screen.path",
value: serializePath(currentViewPath)
)
}
}

private func serializePath(_ path: [String]) -> String {
return path
.filter { str in
!str.starts(with: ("_"))
}
.joined(separator: "/")
}

public func onEnd(span: any ReadableSpan) {}

Expand Down
17 changes: 6 additions & 11 deletions Sources/Honeycomb/UIKit/UIViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
}

private var viewName: String {
return self.view.accessibilityIdentifier ?? self.storyboardId ?? self.title
return self.view.accessibilityIdentifier
?? self.storyboardId
?? self.title
?? NSStringFromClass(type(of: self))
}

Expand All @@ -21,14 +23,6 @@
return [self.viewName]
}

private func viewPath() -> String {
self.viewStack()
.filter { str in
!str.starts(with: ("_"))
}
.joined(separator: "/")
}

private func setAttributes(span: Span, className: String, animated: Bool) {
if let title = self.title {
span.setAttribute(key: "view.title", value: title)
Expand All @@ -45,11 +39,12 @@

// Internal classes from SwiftUI will likely begin with an underscore
if !className.hasPrefix("_") {
// set this _before_ creating the span
HoneycombNavigationProcessor.shared.setCurrentNavigationPath(viewStack())

let span = getUIKitViewTracer().spanBuilder(spanName: "viewDidAppear").startSpan()
setAttributes(span: span, className: className, animated: animated)
span.end()

HoneycombNavigationProcessor.shared.setCurrentNavigationPath(viewPath())
}

traceViewDidAppear(animated)
Expand Down
27 changes: 19 additions & 8 deletions smoke-tests/smoke-e2e.bats
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ mk_attr() {
result=$(attributes_from_span_named $scope $span | jq .key | sort | uniq)

assert_equal "$result" '"screen.name"
"screen.path"
"signpost.category"
"signpost.count"
"signpost.cpu_time"
Expand Down Expand Up @@ -227,23 +228,33 @@ mk_diag_attr() {
assert_not_empty $(spans_on_view_named "@honeycombio/instrumentation-uikit" "Touch Ended" "accessibleButton")
# UISwitch does not support Touch Ended events at this time. Apple sets the view to null.

screen_attr=$(attribute_for_span_key "@honeycombio/instrumentation-uikit" "Touch Began" "screen.name" string | uniq)
screen_attr=$(attributes_from_span_named "@honeycombio/instrumentation-uikit" "Touch Began" \
screen_name_attr=$(attributes_from_span_named "@honeycombio/instrumentation-uikit" "Touch Began" \
| jq "select (.key == \"screen.name\")" \
| jq "select (.value.stringValue == \"SwiftUI.UIKitTabBarController/UIKitNavigationRoot/UI KIT SCREEN OVERRIDE\").value.stringValue" \
| jq "select (.value.stringValue == \"UI KIT SCREEN OVERRIDE\").value.stringValue" \
| uniq
)
assert_equal "$screen_attr" '"SwiftUI.UIKitTabBarController/UIKitNavigationRoot/UI KIT SCREEN OVERRIDE"'
assert_equal "$screen_name_attr" '"UI KIT SCREEN OVERRIDE"'

screen_attr=$(attribute_for_span_key "@honeycombio/instrumentation-uikit" "Touch Began" "screen.name" string | uniq)
screen_path_attr=$(attributes_from_span_named "@honeycombio/instrumentation-uikit" "Touch Began" \
| jq "select (.key == \"screen.path\")" \
| jq "select (.value.stringValue == \"SwiftUI.UIKitTabBarController/UIKitNavigationRoot/UI KIT SCREEN OVERRIDE\").value.stringValue" \
| uniq
)
assert_equal "$screen_path_attr" '"SwiftUI.UIKitTabBarController/UIKitNavigationRoot/UI KIT SCREEN OVERRIDE"'

echo $(attributes_from_span_named "@honeycombio/instrumentation-uikit" "Touch Began" | jq "select (.key == \"screen.name\")")
screen_attr=$(attributes_from_span_named "@honeycombio/instrumentation-uikit" "Touch Began" \
screen_name_attr=$(attributes_from_span_named "@honeycombio/instrumentation-uikit" "Touch Began" \
| jq "select (.key == \"screen.name\")" \
| jq "select (.value.stringValue == \"UIKit Menu\").value.stringValue" \
| uniq
)
assert_equal "$screen_name_attr" '"UIKit Menu"'

screen_path_attr=$(attributes_from_span_named "@honeycombio/instrumentation-uikit" "Touch Began" \
| jq "select (.key == \"screen.path\")" \
| jq "select (.value.stringValue == \"SwiftUI.UIKitTabBarController/UIKitNavigationRoot/UIKit Menu\").value.stringValue" \
| uniq
)
assert_equal "$screen_attr" '"SwiftUI.UIKitTabBarController/UIKitNavigationRoot/UIKit Menu"'
assert_equal "$screen_path_attr" '"SwiftUI.UIKitTabBarController/UIKitNavigationRoot/UIKit Menu"'
}

@test "UIKit click events are captured" {
Expand Down

0 comments on commit 6dd5460

Please sign in to comment.