Skip to content

Commit ebc2e24

Browse files
feat: refactor instrumentation options (#77)
<!-- CURSOR_SUMMARY --> > [!NOTE] > Adds an `Options.Instrumentation` config with per-instrument FeatureFlags and updates client factory to use it, with ExampleApp enabling specific instruments. > > - **API (Options)**: > - Add `FeatureFlag.isEnabled` helper. > - Introduce `Options.Instrumentation` with flags for `urlSession`, `userTaps`, `memory`, `memoryWarnings`, `cpu`, `launchTimes`. > - Extend `Options` with new `instrumentation` property and initializer parameter (default `.init()`). > - **Client Factory**: > - Replace `autoInstrumentation.contains(...)` checks with `options.instrumentation.<instrument>.isEnabled` for `memoryWarnings`, `urlSession`, `launchTimes`, `memory`, and `cpu`. > - **ExampleApp**: > - Configure `options.instrumentation` to enable `urlSession`, `userTaps`, `memory`, `memoryWarnings`, `launchTimes` and disable `cpu`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit dfd4291. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent b442cbc commit ebc2e24

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

ExampleApp/ExampleApp/Client.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ struct Client {
1717
tracesApi: .enabled,
1818
metricsApi: .enabled,
1919
crashReporting: .disabled,
20-
autoInstrumentation: [.urlSession, .userTaps, .memory, .cpu, .memoryWarnings]
20+
autoInstrumentation: [.urlSession, .userTaps, .memory, .cpu, .memoryWarnings],
21+
instrumentation: .init(
22+
urlSession: .enabled,
23+
userTaps: .enabled,
24+
memory: .enabled,
25+
memoryWarnings: .enabled,
26+
cpu: .disabled,
27+
launchTimes: .enabled
28+
)
2129
)
2230
)
2331
]

Sources/Observability/Api/Options.swift

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ public struct Options {
130130
public enum FeatureFlag {
131131
case enabled
132132
case disabled
133+
134+
var isEnabled: Bool {
135+
switch self {
136+
case .enabled: return true
137+
case .disabled: return false
138+
}
139+
}
133140
}
134141
public enum TracingOriginsOption {
135142
case enabled([String])
@@ -144,6 +151,30 @@ public struct Options {
144151
case cpu
145152
case launchTimes
146153
}
154+
public struct Instrumentation {
155+
let urlSession: FeatureFlag
156+
let userTaps: FeatureFlag
157+
let memory: FeatureFlag
158+
let memoryWarnings: FeatureFlag
159+
let cpu: FeatureFlag
160+
let launchTimes: FeatureFlag
161+
162+
public init(
163+
urlSession: FeatureFlag = .disabled,
164+
userTaps: FeatureFlag = .disabled,
165+
memory: FeatureFlag = .disabled,
166+
memoryWarnings: FeatureFlag = .disabled,
167+
cpu: FeatureFlag = .disabled,
168+
launchTimes: FeatureFlag = .disabled
169+
) {
170+
self.urlSession = urlSession
171+
self.userTaps = userTaps
172+
self.memory = memory
173+
self.memoryWarnings = memoryWarnings
174+
self.cpu = cpu
175+
self.launchTimes = launchTimes
176+
}
177+
}
147178
public var serviceName: String
148179
public var serviceVersion: String
149180
public var otlpEndpoint: String
@@ -161,6 +192,7 @@ public struct Options {
161192
public var log: OSLog
162193
public var crashReporting: FeatureFlag
163194
public var autoInstrumentation: Set<AutoInstrumented>
195+
public var instrumentation: Instrumentation
164196
let launchMeter = LaunchMeter()
165197

166198
public init(
@@ -180,7 +212,8 @@ public struct Options {
180212
metricsApi: AppMetrics = .enabled,
181213
log: OSLog = OSLog(subsystem: "com.launchdarkly", category: "LaunchDarklyObservabilityPlugin"),
182214
crashReporting: FeatureFlag = .enabled,
183-
autoInstrumentation: Set<AutoInstrumented> = [.urlSession]
215+
autoInstrumentation: Set<AutoInstrumented> = [.urlSession],
216+
instrumentation: Instrumentation = .init()
184217
) {
185218
self.serviceName = serviceName
186219
self.serviceVersion = serviceVersion
@@ -199,5 +232,6 @@ public struct Options {
199232
self.log = log
200233
self.crashReporting = crashReporting
201234
self.autoInstrumentation = autoInstrumentation
235+
self.instrumentation = instrumentation
202236
}
203237
}

Sources/Observability/Client/ObservabilityClientFactory.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public struct ObservabilityClientFactory {
4848
Task {
4949
await batchWorker.addExporter(logExporter)
5050
}
51-
if options.autoInstrumentation.contains(.memoryWarnings) {
51+
if options.instrumentation.memoryWarnings.isEnabled {
5252
let memoryWarningMonitor = MemoryPressureMonitor(options: options, appLogBuilder: appLogBuilder) { log in
5353
await eventQueue.send(LogItem(log: log))
5454
}
@@ -88,7 +88,7 @@ public struct ObservabilityClientFactory {
8888
options: options.tracesApi,
8989
tracingApiClient: traceClient
9090
)
91-
if options.autoInstrumentation.contains(.urlSession) {
91+
if options.instrumentation.urlSession.isEnabled {
9292
autoInstrumentation.append(
9393
NetworkInstrumentationManager(
9494
options: options,
@@ -97,7 +97,7 @@ public struct ObservabilityClientFactory {
9797
)
9898
)
9999
}
100-
if options.autoInstrumentation.contains(.launchTimes) {
100+
if options.instrumentation.launchTimes.isEnabled {
101101
options.launchMeter.subscribe { statistics in
102102
for element in statistics {
103103
let span = traceClient.startSpan(
@@ -141,7 +141,7 @@ public struct ObservabilityClientFactory {
141141
metricsApiClient: metricsClient
142142
)
143143

144-
if options.autoInstrumentation.contains(.memory) {
144+
if options.instrumentation.memory.isEnabled {
145145
autoInstrumentation.append(
146146
MeasurementTask(metricsApi: metricsClient, samplingInterval: autoInstrumentationSamplingInterval) { api in
147147
guard let report = MemoryUseManager.memoryReport(log: options.log) else { return }
@@ -155,7 +155,7 @@ public struct ObservabilityClientFactory {
155155
)
156156
}
157157

158-
if options.autoInstrumentation.contains(.cpu) {
158+
if options.instrumentation.cpu.isEnabled {
159159
autoInstrumentation.append(
160160
MeasurementTask(metricsApi: metricsClient, samplingInterval: autoInstrumentationSamplingInterval) { api in
161161
guard let value = CpuUtilizationManager.currentCPUUsage() else { return }

0 commit comments

Comments
 (0)