generated from honeycombio/.github
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a382baa
commit 844bdc7
Showing
2 changed files
with
67 additions
and
24 deletions.
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
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 | ||
) | ||
} |