Skip to content

Commit

Permalink
add "real" instrumented view
Browse files Browse the repository at this point in the history
  • Loading branch information
MustafaHaddara committed Nov 1, 2024
1 parent a382baa commit 844bdc7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 24 deletions.
36 changes: 12 additions & 24 deletions Examples/SmokeTest/SmokeTest/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,18 @@ private func flush() {
Thread.sleep(forTimeInterval: 3.0)
}

struct InstrumentedView<Content: View>: View {
private let creationDate: Date
private let content: () -> Content

init(@SwiftUICore.ViewBuilder _ content: @escaping () -> Content) {
self.creationDate = Date()
self.content = content
}

var body: some View {
content()
.onAppear {
let interval = Date().timeIntervalSince(creationDate)
print("render time: \(interval)")
}
}
}

struct ContentView: View {
var body: some View {
InstrumentedView {
HoneycombInstrumentedView(name: "main view") {
VStack(
alignment: .center,
spacing: 20.0
) {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
HoneycombInstrumentedView(name: "home icon") {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
}

Text("This is a sample app.")

Expand All @@ -77,14 +61,18 @@ struct ContentView: View {
}
.buttonStyle(.bordered)

Text(String(timeConsumingCalculation()))
HoneycombInstrumentedView(name: "expensive text") {
Text(String(timeConsumingCalculation()))
}

}
.padding()
}
}

private func timeConsumingCalculation() -> Int {
(1...10_000_000).reduce(0, +)
print("starting time consuming calculation")
return (1...100_000_000).reduce(0, +)
}
}

Expand Down
55 changes: 55 additions & 0 deletions Sources/Honeycomb/HoneycombInstrumentedView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import OpenTelemetryApi
import OpenTelemetrySdk
import SwiftUI

private let honeycombInstrumentedViewName = "@honeycombio/instrumentation-view"

struct HoneycombInstrumentedView<Content: View>: View {
private let span: Span
private let content: () -> Content
private let name: String

init(name: String, @SwiftUICore.ViewBuilder _ content: @escaping () -> Content) {
self.name = name
self.content = content

span = getMetricKitTracer().spanBuilder(spanName: "ViewInstrumentation")
.setStartTime(time: Date())
.setAttribute(key: "ViewName", value: name)
.startSpan()

print("\(name) init")
}

var body: some View {
print("\(name) started rendering")
let start = Date()

let c = content()

print("\(name) finished rendering")
span.setAttribute(
key: "RenderTimeMicroSeconds",
value: Int(Date().timeIntervalSince(start).toMicroseconds)
)

return c.onAppear {
span.end(time: Date())
}
}
}

extension View {
func honeycombInstrumentedView(name: String) -> some View {
HoneycombInstrumentedView(name: name) {
self
}
}
}

func getViewTracer() -> Tracer {
return OpenTelemetry.instance.tracerProvider.get(
instrumentationName: honeycombInstrumentedViewName,
instrumentationVersion: honeycombLibraryVersion
)
}

0 comments on commit 844bdc7

Please sign in to comment.