Skip to content
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

RUM-5757 benchmark: Collect Session Replay Record Spans #2044

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion BenchmarkTests/Benchmarks/Sources/Benchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,25 @@ public enum Benchmarks {
environment: "benchmarks",
apiKey: configuration.apiKey,
endpoint: .us1,
uploadCondition: { true }
uploadCondition: { true },
performancePreset: .instantDataDelivery
)

let exporter = try! DatadogExporter(config: exporterConfiguration)
let processor = SimpleSpanProcessor(spanExporter: exporter)

let provider = TracerProviderBuilder()
.with(resource: Resource(attributes: [
"device_model": configuration.context.deviceModel,
"os": configuration.context.osName,
"os_version": configuration.context.osVersion,
"run": configuration.context.run,
"scenario": configuration.context.scenario,
"sdk_version": configuration.context.sdkVersion,
"branch": configuration.context.branch,
].mapValues { .string($0) }))
.add(spanProcessor: processor)
.with(sampler: Samplers.traceIdRatio(ratio: 0.01))
.build()

OpenTelemetry.registerTracerProvider(tracerProvider: provider)
Expand Down
40 changes: 34 additions & 6 deletions BenchmarkTests/Runner/BenchmarkProfiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,50 @@
*/

import Foundation

import DatadogInternal
import DatadogBenchmarks
import OpenTelemetryApi
import OpenTelemetrySdk

internal final class Profiler: DatadogInternal.BenchmarkProfiler {
let opentelemetry: OpenTelemetry

init(opentelemetry: OpenTelemetry = .instance) {
self.opentelemetry = opentelemetry
}

func tracer(operation: @autoclosure () -> String) -> any DatadogInternal.BenchmarkTracer {
DummyTracer()
TracerWrapper(
tracer: opentelemetry.tracerProvider.get(instrumentationName: operation(), instrumentationVersion: nil)
)
}
}

internal final class DummyTracer: DatadogInternal.BenchmarkTracer {
private final class TracerWrapper: DatadogInternal.BenchmarkTracer {
let tracer: OpenTelemetryApi.Tracer

init(tracer: OpenTelemetryApi.Tracer) {
self.tracer = tracer
}

func startSpan(named: @autoclosure () -> String) -> any DatadogInternal.BenchmarkSpan {
DummySpan()
SpanWrapper(
span: tracer
.spanBuilder(spanName: named())
.setActive(true)
.startSpan()
)
}
}

internal final class DummySpan: DatadogInternal.BenchmarkSpan {
func stop() { }
private final class SpanWrapper: DatadogInternal.BenchmarkSpan {
let span: OpenTelemetryApi.Span

init(span: OpenTelemetryApi.Span) {
self.span = span
}

func stop() {
span.end()
}
}
8 changes: 8 additions & 0 deletions DatadogInternal/Sources/Benchmarks/BenchmarkProfiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,11 @@ private final class NOPBenchmarkProfiler: BenchmarkProfiler, BenchmarkTracer, Be
/// no-op
func stop() {}
}

public final class NOPBenchmarkTracer: BenchmarkTracer, BenchmarkSpan {
public init() {}
/// no-op
public func startSpan(named: @autoclosure () -> String) -> BenchmarkSpan { self }
/// no-op
public func stop() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ internal struct ViewTreeRecorder {
view: UIView,
context: ViewTreeRecordingContext
) {
let span = context.benchmarkTracer.startSpan(named: "<UIView>")
defer { span.stop() }

var context = context
if let viewController = view.next as? UIViewController {
context.viewControllerContext.parentType = .init(viewController)
Expand Down Expand Up @@ -61,6 +64,9 @@ internal struct ViewTreeRecorder {
var semantics: NodeSemantics = UnknownElement.constant

for nodeRecorder in nodeRecorders {
let span = context.benchmarkTracer.startSpan(named: String(describing: type(of: nodeRecorder)))
defer { span.stop() }

guard let nextSemantics = nodeRecorder.semantics(of: view, with: attributes, in: context) else {
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
import SafariServices
import SwiftUI
import WebKit
import DatadogInternal

/// The context of recording subtree hierarchy.
///
Expand All @@ -25,6 +26,8 @@ public struct SessionReplayViewTreeRecordingContext {
var viewControllerContext: ViewControllerContext = .init()
/// Webviews caching.
let webViewCache: NSHashTable<WKWebView>

let benchmarkTracer: BenchmarkTracer
}

// This alias enables us to have a more unique name exposed through public-internal access level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation
import UIKit
import WebKit
import DatadogInternal

/// Builds `ViewTreeSnapshot` for given root view.
///
Expand All @@ -20,6 +21,8 @@ internal struct ViewTreeSnapshotBuilder {
/// The webviews cache.
let webViewCache: NSHashTable<WKWebView> = .weakObjects()

let benchmarkTracer: BenchmarkTracer = profiler.tracer(operation: "ViewTreeSnapshotBuilder")

/// Builds the `ViewTreeSnapshot` for given root view.
///
/// - Parameter rootView: the root view
Expand All @@ -32,9 +35,14 @@ internal struct ViewTreeSnapshotBuilder {
recorder: recorderContext,
coordinateSpace: rootView,
ids: idsGenerator,
webViewCache: webViewCache
webViewCache: webViewCache,
benchmarkTracer: benchmarkTracer
)

let span = benchmarkTracer.startSpan(named: "record")
let nodes = viewTreeRecorder.record(rootView, in: context)
span.stop()

let snapshot = ViewTreeSnapshot(
date: recorderContext.date.addingTimeInterval(recorderContext.viewServerTimeOffset ?? 0),
context: recorderContext,
Expand Down
7 changes: 5 additions & 2 deletions DatadogSessionReplay/Tests/Mocks/RecorderMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import XCTest
import UIKit
import WebKit

import DatadogInternal
@_spi(Internal)
@testable import DatadogSessionReplay
@testable import TestUtilities
Expand Down Expand Up @@ -337,7 +338,8 @@ extension ViewTreeRecordingContext: AnyMockable, RandomMockable {
recorder: .mockRandom(),
coordinateSpace: UIView.mockRandom(),
ids: NodeIDGenerator(),
webViewCache: .weakObjects()
webViewCache: .weakObjects(),
benchmarkTracer: NOPBenchmarkTracer()
)
}

Expand All @@ -351,7 +353,8 @@ extension ViewTreeRecordingContext: AnyMockable, RandomMockable {
recorder: recorder,
coordinateSpace: coordinateSpace,
ids: ids,
webViewCache: webViewCache
webViewCache: webViewCache,
benchmarkTracer: NOPBenchmarkTracer()
)
}
}
Expand Down