Skip to content

Commit 500f550

Browse files
feat: set sampling config via graphql client (#19)
- add flush API - add Disable instrumentation logs, tracers, metrics - refactor instrumentation manager
1 parent 28713fc commit 500f550

File tree

16 files changed

+737
-420
lines changed

16 files changed

+737
-420
lines changed

ExampleApp/ExampleApp/AppDelegate.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ let config = { () -> LDConfig in
99
autoEnvAttributes: .enabled
1010
)
1111
config.plugins = [
12-
Observability(options: .init(sessionBackgroundTimeout: 3, isDebug: true))
12+
Observability(
13+
options: .init(
14+
// otlpEndpoint: "http://localhost:4318",
15+
sessionBackgroundTimeout: 3,
16+
isDebug: true,
17+
disableLogs: false,
18+
disableTraces: false,
19+
disableMetrics: false
20+
)
21+
)
1322
]
1423
return config
1524
}()

Package.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ let package = Package(
7272
.copy("Resources/Stubs/MinConfig.json")
7373
]
7474
),
75+
.target(
76+
name: "Instrumentation",
77+
dependencies: [
78+
"API",
79+
"CrashReporter",
80+
"Sampling",
81+
.product(name: "OpenTelemetrySdk", package: "opentelemetry-swift"),
82+
.product(name: "OpenTelemetryApi", package: "opentelemetry-swift"),
83+
]
84+
),
7585
.target(
7686
name: "Observability",
7787
dependencies: [
@@ -81,13 +91,14 @@ let package = Package(
8191
"CrashReporterLive",
8292
"Sampling",
8393
"SamplingLive",
94+
"Instrumentation",
8495
.product(name: "OpenTelemetrySdk", package: "opentelemetry-swift"),
8596
.product(name: "OpenTelemetryApi", package: "opentelemetry-swift"),
8697
.product(name: "OpenTelemetryProtocolExporterHTTP", package: "opentelemetry-swift"),
8798
.product(name: "URLSessionInstrumentation", package: "opentelemetry-swift"),
8899
],
89100
resources: [
90-
.copy("Resources/Config.json"),
101+
.process("Resources"),
91102
]
92103
),
93104
.target(

Sources/API/Observe.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ public protocol Observe {
5454
*/
5555
func startSpan(name: String, attributes: [String: AttributeValue]) -> Span
5656

57-
func flush()
57+
func flush() -> Bool
5858
}

Sources/Common/LDLogger.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import OSLog
2+
3+
public final class ObservabilityLogger {
4+
public let log: OSLog
5+
6+
public init(
7+
name: String = "observability-sdk"
8+
) {
9+
self.log = OSLog(subsystem: "com.launchdarkly", category: name)
10+
}
11+
}

Sources/CrashReporterLive/LiveValue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension CrashReporter {
3434
let installation = CrashInstallationStandard.shared
3535
let config = KSCrashConfiguration()
3636

37-
config.deadlockWatchdogInterval = 5.0
37+
config.deadlockWatchdogInterval = 0
3838
config.enableMemoryIntrospection = true
3939
config.monitors = .all
4040
config.enableSigTermMonitoring = true
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import OpenTelemetryApi
2+
3+
import API
4+
import Sampling
5+
6+
public struct Instrumentation {
7+
public var recordMetric: (_ metric: Metric) -> Void
8+
public var recordCount: (_ metric: Metric) -> Void
9+
public var recordIncr: (_ metric: Metric) -> Void
10+
public var recordHistogram: (_ metric: Metric) -> Void
11+
public var recordUpDownCounter: (_ metric: Metric) -> Void
12+
public var recordError: (_ error: Error, _ attributes: [String: AttributeValue]) -> Void
13+
public var recordLog: (_ message: String, _ severity: Severity, _ attributes: [String: AttributeValue]) -> Void
14+
public var startSpan: (_ name: String, _ attributes: [String: AttributeValue]) -> Span
15+
public var flush: () -> Bool
16+
17+
public init(
18+
recordMetric: @escaping (_: Metric) -> Void,
19+
recordCount: @escaping (_: Metric) -> Void,
20+
recordIncr: @escaping (_: Metric) -> Void,
21+
recordHistogram: @escaping (_: Metric) -> Void,
22+
recordUpDownCounter: @escaping (_: Metric) -> Void,
23+
recordError: @escaping (_: Error, _: [String : AttributeValue]) -> Void,
24+
recordLog: @escaping (_: String, _: Severity, _: [String : AttributeValue]) -> Void,
25+
startSpan: @escaping (_: String, _: [String : AttributeValue]) -> Span,
26+
flush: @escaping () -> Bool
27+
) {
28+
self.recordMetric = recordMetric
29+
self.recordCount = recordCount
30+
self.recordIncr = recordIncr
31+
self.recordHistogram = recordHistogram
32+
self.recordUpDownCounter = recordUpDownCounter
33+
self.recordError = recordError
34+
self.recordLog = recordLog
35+
self.startSpan = startSpan
36+
self.flush = flush
37+
}
38+
39+
public func recordMetric(metric: Metric) {
40+
recordMetric(metric)
41+
}
42+
43+
public func recordCount(metric: Metric) {
44+
recordCount(metric)
45+
}
46+
47+
public func recordIncr(metric: Metric) {
48+
recordIncr(metric)
49+
}
50+
51+
public func recordHistogram(metric: Metric) {
52+
recordHistogram(metric)
53+
}
54+
55+
public func recordUpDownCounter(metric: Metric) {
56+
recordUpDownCounter(metric)
57+
}
58+
59+
public func recordError(error: Error, attributes: [String: AttributeValue]) {
60+
recordError(error, attributes)
61+
}
62+
63+
public func recordLog(message: String, severity: Severity, attributes: [String: AttributeValue]) {
64+
recordLog(message, severity, attributes)
65+
}
66+
67+
public func startSpan(name: String, attributes: [String: AttributeValue]) -> Span {
68+
startSpan(name, attributes)
69+
}
70+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public enum InstrumentationError: Error {
2+
case graphQLUrlIsInvalid
3+
}

Sources/LaunchDarklyObservability/LDObserve.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ private final class NoOpClient: Observe {
1212
func recordError(error: any Error, attributes: [String : AttributeValue]) {}
1313
func recordLog(message: String, severity: Severity, attributes: [String : AttributeValue]) {}
1414
func startSpan(name: String, attributes: [String : AttributeValue]) -> any Span {
15-
OpenTelemetry.instance.tracerProvider.get(
16-
instrumentationName: "",
17-
instrumentationVersion: ""
18-
).spanBuilder(spanName: "").startSpan()
15+
/// No-op implementation of the Tracer
16+
DefaultTracer.instance.spanBuilder(spanName: "").startSpan()
1917
}
20-
func flush() {}
18+
func flush() -> Bool { true }
2119
}
2220

2321
public final class LDObserve: @unchecked Sendable, Observe {
@@ -83,9 +81,9 @@ public final class LDObserve: @unchecked Sendable, Observe {
8381
return client.startSpan(name: name, attributes: attributes)
8482
}
8583

86-
public func flush() {
84+
public func flush() -> Bool {
8785
lock.lock()
8886
defer { lock.unlock() }
89-
client.flush()
87+
return client.flush()
9088
}
9189
}

Sources/LaunchDarklyObservability/Plugin/Observability.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ public final class Observability: Plugin {
4343
loggerName: options.loggerName
4444
)
4545
let client = ObservabilityClient(
46-
sdkKey: sdkKey,
47-
resource: .init(attributes: resourceAttributes),
48-
options: options
46+
context: .init(
47+
sdkKey: sdkKey,
48+
resource: .init(attributes: resourceAttributes),
49+
options: options,
50+
logger: .init(name: options.loggerName)
51+
)
4952
)
5053

5154
LDObserve.shared.set(client: client)

0 commit comments

Comments
 (0)