Skip to content

Commit fb9d783

Browse files
feat: sampling api
1 parent dc91aa5 commit fb9d783

32 files changed

+2424
-18
lines changed

ExampleApp/ExampleApp/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class AppDelegate: NSObject, UIApplicationDelegate {
4646
_ application: UIApplication,
4747
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
4848
) -> Bool {
49-
print(once)
49+
_ = once
5050
return true
5151
}
5252
}

ExampleApp/ExampleApp/ContentView.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,32 @@
88
import SwiftUI
99

1010
struct ContentView: View {
11+
@Environment(Browser.self) var browser
12+
1113
var body: some View {
1214
VStack(spacing: 32) {
1315
Button {
1416
fatalError()
1517
} label: {
1618
Text("Crash")
1719
}
18-
NetworkRequestView()
19-
FeatureFlagView()
20+
Button {
21+
browser.navigate(to: .automaticInstrumentation)
22+
} label: {
23+
Text("Automatic Instrumentation")
24+
}
25+
Button {
26+
browser.navigate(to: .evaluation)
27+
} label: {
28+
Text("Flag evaluation")
29+
}
30+
Button {
31+
browser.navigate(to: .manualInstrumentation)
32+
} label: {
33+
Text("Manual Instrumentation")
34+
}
35+
// NetworkRequestView()
36+
// FeatureFlagView()
2037
}
2138
.padding()
2239
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import SwiftUI
2+
3+
struct DMButtonStyle: ButtonStyle {
4+
func makeBody(configuration: Configuration) -> some View {
5+
configuration.label
6+
.padding()
7+
.background(Color.blue)
8+
.foregroundColor(.white)
9+
.cornerRadius(8)
10+
.bold()
11+
.frame(width: 100)
12+
.opacity(configuration.isPressed ? 0.8 : 1)
13+
}
14+
}
15+
16+
extension ButtonStyle where Self == DMButtonStyle {
17+
static var ldStyle: Self { .init() }
18+
}

ExampleApp/ExampleApp/ExampleAppApp.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,27 @@ import SwiftUI
33
@main
44
struct ExampleAppApp: App {
55
@UIApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate
6+
@State private var browser = Browser()
7+
68

79
var body: some Scene {
810
WindowGroup {
9-
ContentView()
11+
NavigationStack(path: $browser.path) {
12+
ContentView()
13+
.navigationDestination(for: Path.self) { path in
14+
switch path {
15+
case .home:
16+
ContentView()
17+
case .manualInstrumentation:
18+
InstrumentationView()
19+
case .automaticInstrumentation:
20+
NetworkRequestView()
21+
case .evaluation:
22+
FeatureFlagView()
23+
}
24+
}
25+
}
26+
.environment(browser)
1027
}
1128
}
1229
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import SwiftUI
2+
import LaunchDarklyObservability
3+
import OpenTelemetryApi
4+
5+
struct InstrumentationView: View {
6+
var body: some View {
7+
VStack {
8+
TraceView()
9+
}
10+
.padding()
11+
}
12+
}
13+
14+
#Preview {
15+
InstrumentationView()
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import SwiftUI
2+
import LaunchDarklyObservability
3+
import OpenTelemetryApi
4+
5+
6+
struct TraceView: View {
7+
@State private var name: String = ""
8+
@State private var started = false
9+
@State private var span = Optional<Span>.none
10+
11+
var body: some View {
12+
VStack(alignment: .leading, spacing: 4) {
13+
Text("Traces")
14+
.bold()
15+
HStack {
16+
TextField(text: $name) {
17+
Text("Span name:")
18+
}
19+
.textCase(.lowercase)
20+
.textInputAutocapitalization(.never)
21+
.textFieldStyle(.roundedBorder)
22+
Spacer()
23+
Text("is started")
24+
Toggle(isOn: $started) {
25+
Text("started")
26+
}
27+
.labelsHidden()
28+
.disabled(name.isEmpty)
29+
.task(id: started) {
30+
guard started else {
31+
span?.end()
32+
return name = ""
33+
}
34+
span = LDObserve.shared.startSpan(name: name)
35+
}
36+
}
37+
}
38+
}
39+
}

ExampleApp/ExampleApp/Path.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import SwiftUI
2+
3+
enum Path: Hashable {
4+
case home
5+
case manualInstrumentation
6+
case automaticInstrumentation
7+
case evaluation
8+
}
9+
10+
@Observable final class Browser {
11+
var path = NavigationPath()
12+
13+
func navigate(to path: Path) {
14+
self.path.append(path)
15+
}
16+
17+
func pop() {
18+
self.path.removeLast()
19+
}
20+
21+
func reset() {
22+
self.path = NavigationPath()
23+
}
24+
}

Package.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,50 @@ let package = Package(
3434
.product(name: "Installations", package: "KSCrash")
3535
]
3636
),
37+
.target(
38+
name: "Sampling",
39+
dependencies: [
40+
.product(name: "OpenTelemetryApi", package: "opentelemetry-swift"),
41+
.product(name: "OpenTelemetrySdk", package: "opentelemetry-swift")
42+
]
43+
),
44+
.target(
45+
name: "SamplingLive",
46+
dependencies: [
47+
"Sampling",
48+
"Common",
49+
.product(name: "OpenTelemetryApi", package: "opentelemetry-swift"),
50+
.product(name: "OpenTelemetrySdk", package: "opentelemetry-swift")
51+
]
52+
),
53+
.testTarget(
54+
name: "SamplingLiveTests",
55+
dependencies: [
56+
"Sampling",
57+
"SamplingLive",
58+
"Common",
59+
.product(name: "OpenTelemetryApi", package: "opentelemetry-swift"),
60+
.product(name: "OpenTelemetrySdk", package: "opentelemetry-swift")
61+
],
62+
resources: [
63+
.copy("Resources/Stubs/Config.json")
64+
]
65+
),
3766
.target(
3867
name: "Observability",
3968
dependencies: [
4069
"Common",
4170
"API",
4271
"CrashReporter",
4372
"CrashReporterLive",
73+
"Sampling",
74+
"SamplingLive",
4475
.product(name: "OpenTelemetrySdk", package: "opentelemetry-swift"),
4576
.product(name: "OpenTelemetryProtocolExporterHTTP", package: "opentelemetry-swift"),
4677
.product(name: "URLSessionInstrumentation", package: "opentelemetry-swift"),
78+
],
79+
resources: [
80+
.copy("Resources/Config.json")
4781
]
4882
),
4983
.target(
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
public enum SemanticConvention: String {
2-
case highlightSessionId = "highlight.session_id"
1+
public enum SemanticConvention {
2+
public static let highlightSessionId = "highlight.session_id"
3+
}
4+
5+
public enum LDSemanticAttribute {
6+
public static let ATTR_SAMPLING_RATIO = "launchdarkly.sampling.ratio"
37
}

0 commit comments

Comments
 (0)