generated from honeycombio/.github
-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: UIKit Navigation instrimentation #22
Merged
arriIsHere
merged 28 commits into
main
from
arriIsHere.ui-navigation-controller-instrumentation
Nov 22, 2024
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
0819b0b
create navigation instrumentation class
arriIsHere 04ce8d9
add first swizzle impl
arriIsHere e95e6b4
rename and move methods
arriIsHere 96d5078
Move files around and rename to match convention
arriIsHere 43d477f
add storyboard file for UI kit tab
arriIsHere 4a17ec3
Shim for SwiftUI to UIKit
arriIsHere b7b5d12
Add UIKit tab
arriIsHere 13b93ef
Add test for UIKit
arriIsHere 809b582
fix and add instrumentation
arriIsHere 1cdb040
update storyboard and smoke tests
arriIsHere aa274e0
fix formatting
arriIsHere b61fc95
update to pass
arriIsHere 65a8d2a
add disappear test
arriIsHere 35fd328
exactly one line, no count needed
arriIsHere e11980b
fix method names
arriIsHere bbd647d
filter out generated SwiftUI classes
arriIsHere 13ef5f3
fix trailing ws
arriIsHere 807feb2
title is only added when present
arriIsHere ca94603
comment spelling error
arriIsHere aae1c71
also exclude on disappear
arriIsHere bdc06a5
rename views
arriIsHere 22b2e9a
Remove duplicate methods and fields
arriIsHere 6967340
update smoke
arriIsHere a11a82e
use own instrumentation name
arriIsHere 81736a7
test for existing tab view
arriIsHere 48a8ef8
UIKit documentation
arriIsHere 9c18514
add nib name
arriIsHere 8586f74
clean up nits
arriIsHere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="32700.99.1234" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES"> | ||
<device id="retina6_12" orientation="portrait" appearance="light"/> | ||
<dependencies> | ||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22685"/> | ||
<capability name="Safe area layout guides" minToolsVersion="9.0"/> | ||
<capability name="System colors in document resources" minToolsVersion="11.0"/> | ||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> | ||
</dependencies> | ||
<scenes> | ||
<!--View Controller--> | ||
<scene sceneID="s0d-6b-0kx"> | ||
<objects> | ||
<viewController storyboardIdentifier="UIKitView" id="Y6W-OH-hqX" customClass="ViewController" sceneMemberID="viewController"> | ||
<view key="view" contentMode="scaleToFill" id="5EZ-qb-Rvc"> | ||
<rect key="frame" x="0.0" y="0.0" width="393" height="852"/> | ||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> | ||
<subviews> | ||
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" fixedFrame="YES" image="globe" catalog="system" translatesAutoresizingMaskIntoConstraints="NO" id="O94-9o-FQ4"> | ||
<rect key="frame" x="169" y="216" width="54" height="46"/> | ||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> | ||
</imageView> | ||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Sample UIKit App" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="008-k5-M1T"> | ||
<rect key="frame" x="130" y="266" width="133" height="21"/> | ||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> | ||
<fontDescription key="fontDescription" type="system" pointSize="17"/> | ||
<nil key="textColor"/> | ||
<nil key="highlightedColor"/> | ||
</label> | ||
</subviews> | ||
<viewLayoutGuide key="safeArea" id="vDu-zF-Fre"/> | ||
<color key="backgroundColor" systemColor="systemBackgroundColor"/> | ||
</view> | ||
</viewController> | ||
<placeholder placeholderIdentifier="IBFirstResponder" id="Ief-a0-LHa" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/> | ||
</objects> | ||
<point key="canvasLocation" x="61.832061068702288" y="3.5211267605633805"/> | ||
</scene> | ||
</scenes> | ||
<resources> | ||
<image name="globe" catalog="system" width="128" height="123"/> | ||
<systemColor name="systemBackgroundColor"> | ||
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/> | ||
</systemColor> | ||
</resources> | ||
</document> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Foundation | ||
import SwiftUI | ||
import UIKit | ||
|
||
struct UIKitView: View { | ||
var body: some View { | ||
StoryboardViewControllerRepresentation() | ||
} | ||
} | ||
|
||
struct UIKView_preview: PreviewProvider { | ||
static var previews: some View { | ||
UIKitView() | ||
} | ||
} | ||
|
||
struct StoryboardViewControllerRepresentation: UIViewControllerRepresentable { | ||
func makeUIViewController(context: Context) -> some UIViewController { | ||
let storyboard = UIStoryboard(name: "UIKitView", bundle: Bundle.main) | ||
let controller = storyboard.instantiateViewController(identifier: "UIKitView") | ||
return controller | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { | ||
} | ||
} | ||
|
||
class ViewController: UIViewController { | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
// Do any additional setup after loading the view. | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#if canImport(UIKit) | ||
import Foundation | ||
import OpenTelemetryApi | ||
import UIKit | ||
|
||
private let honeycombUIKitInstrumentationName = "@honeycombio/instrumentation-uikit" | ||
|
||
internal func getUIKitViewTracer() -> Tracer { | ||
return OpenTelemetry.instance.tracerProvider.get( | ||
instrumentationName: honeycombUIKitInstrumentationName, | ||
instrumentationVersion: honeycombLibraryVersion | ||
) | ||
} | ||
|
||
public func installUINavigationInstrumentation() { | ||
UIViewController.swizzle() | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#if canImport(UIKit) | ||
import Foundation | ||
import OpenTelemetryApi | ||
import UIKit | ||
|
||
extension UIViewController { | ||
@objc func traceViewDidAppear(_ animated: Bool) { | ||
let className = NSStringFromClass(type(of: self)) | ||
|
||
// Internal classes from SwiftUI will likely begin with an underscore | ||
if !className.hasPrefix("_") { | ||
let span = getUIKitViewTracer().spanBuilder(spanName: "viewDidAppear").startSpan() | ||
if self.title != nil { | ||
span.setAttribute(key: "title", value: self.title!) | ||
} | ||
if self.nibName != nil { | ||
span.setAttribute(key: "nibName", value: self.nibName!) | ||
} | ||
span.setAttribute(key: "animated", value: animated) | ||
span.setAttribute(key: "className", value: className) | ||
|
||
span.end() | ||
} | ||
|
||
traceViewDidAppear(animated) | ||
} | ||
|
||
@objc func traceViewDidDisappear(_ animated: Bool) { | ||
|
||
let className = NSStringFromClass(type(of: self)) | ||
|
||
// Internal classes from SwiftUI will likely begin with an underscore | ||
if !className.hasPrefix("_") { | ||
let span = getUIKitViewTracer().spanBuilder(spanName: "viewDidDisappear") | ||
.startSpan() | ||
if self.title != nil { | ||
span.setAttribute(key: "title", value: self.title!) | ||
} | ||
if self.nibName != nil { | ||
span.setAttribute(key: "nibName", value: self.nibName!) | ||
} | ||
span.setAttribute(key: "animated", value: animated) | ||
span.setAttribute(key: "className", value: className) | ||
span.end() | ||
} | ||
|
||
traceViewDidDisappear(animated) | ||
} | ||
|
||
public static func swizzle() { | ||
let originalAppearSelector = #selector(UIViewController.viewDidAppear(_:)) | ||
let swizzledAppearSelector = #selector(UIViewController.traceViewDidAppear(_:)) | ||
let originalDisappearSelector = #selector(UIViewController.viewDidDisappear(_:)) | ||
let swizzledDisappearSelector = #selector(UIViewController.traceViewDidDisappear(_:)) | ||
|
||
guard | ||
let originalAppearMethod = class_getInstanceMethod(self, originalAppearSelector), | ||
let swizzledAppearMethod = class_getInstanceMethod(self, swizzledAppearSelector) | ||
else { | ||
print("unable to swizzle \(originalAppearSelector): original method not found") | ||
return | ||
} | ||
|
||
method_exchangeImplementations(originalAppearMethod, swizzledAppearMethod) | ||
|
||
guard | ||
let originalDisappearMethod = class_getInstanceMethod( | ||
self, | ||
originalDisappearSelector | ||
), | ||
let swizzledDisappearMethod = class_getInstanceMethod( | ||
self, | ||
swizzledDisappearSelector | ||
) | ||
else { | ||
print("unable to swizzle \(originalDisappearSelector): original method not found") | ||
return | ||
} | ||
|
||
method_exchangeImplementations(originalDisappearMethod, swizzledDisappearMethod) | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're swizzling
UIViewController
, this instrumentation should work for bothUINavigationContoller
andUITabBarController
, among others. Should we add tests for both a navigation controller and a tab view, to make sure they're both reasonable? I'm not sure how annoying that would be to build in Storyboard.