Skip to content

Commit 3d7315a

Browse files
committed
tweaking telemetry inspector usage for testing
1 parent 9e1e15a commit 3d7315a

File tree

4 files changed

+41
-25
lines changed

4 files changed

+41
-25
lines changed

e2e/android/app/src/test/java/com/example/androidobservability/DisablingConfigOptionsE2ETest.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ package com.example.androidobservability
22

33
import android.app.Application
44
import androidx.test.core.app.ApplicationProvider
5+
import com.example.androidobservability.TestUtils.TelemetryType
56
import com.example.androidobservability.TestUtils.waitForTelemetryData
7+
import com.launchdarkly.observability.api.Options
68
import com.launchdarkly.observability.interfaces.Metric
79
import com.launchdarkly.observability.sdk.LDObserve
810
import io.opentelemetry.api.common.AttributeKey
911
import io.opentelemetry.api.common.Attributes
1012
import io.opentelemetry.api.logs.Severity
11-
import com.example.androidobservability.TestUtils.TelemetryType
12-
import com.launchdarkly.observability.api.Options
1313
import junit.framework.TestCase.assertEquals
1414
import junit.framework.TestCase.assertFalse
1515
import junit.framework.TestCase.assertNotNull
16-
import junit.framework.TestCase.assertNull
1716
import junit.framework.TestCase.assertTrue
1817
import org.junit.Test
1918
import org.junit.runner.RunWith
@@ -96,7 +95,6 @@ class DisablingConfigOptionsE2ETest {
9695
LDObserve.flush()
9796
waitForTelemetryData(telemetryInspector = application.telemetryInspector, telemetryType = TelemetryType.METRICS)
9897

99-
assertNull(application.telemetryInspector?.metricExporter)
10098
assertFalse(requestsContainsUrl(metricsUrl))
10199
}
102100

@@ -110,7 +108,6 @@ class DisablingConfigOptionsE2ETest {
110108
LDObserve.flush()
111109
waitForTelemetryData(telemetryInspector = application.telemetryInspector, telemetryType = TelemetryType.METRICS)
112110

113-
assertNotNull(application.telemetryInspector?.metricExporter)
114111
assertTrue(requestsContainsUrl(metricsUrl))
115112
}
116113

sdk/@launchdarkly/observability-android/lib/src/main/kotlin/com/launchdarkly/observability/client/InstrumentationManager.kt

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder
3838
import io.opentelemetry.sdk.metrics.export.MetricExporter
3939
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader
4040
import io.opentelemetry.sdk.resources.Resource
41-
import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter
42-
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricExporter
43-
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter
4441
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder
4542
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor
4643
import io.opentelemetry.sdk.trace.export.SpanExporter
@@ -74,9 +71,6 @@ class InstrumentationManager(
7471
private var customSampler = CustomSampler()
7572
private val graphqlClient = GraphQLClient(options.backendUrl)
7673
private val samplingApiService = SamplingApiService(graphqlClient)
77-
private var inMemorySpanExporter: InMemorySpanExporter? = null
78-
private var inMemoryLogExporter: InMemoryLogRecordExporter? = null
79-
private var inMemoryMetricExporter: InMemoryMetricExporter? = null
8074
private var telemetryInspector: TelemetryInspector? = null
8175
private var spanProcessor: BatchSpanProcessor? = null
8276
private var logProcessor: LogRecordProcessor? = null
@@ -90,6 +84,7 @@ class InstrumentationManager(
9084
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
9185

9286
init {
87+
initializeTelemetryInspector()
9388
val otelRumConfig = createOtelRumConfig()
9489

9590
val builder = OpenTelemetryRum.builder(application, otelRumConfig)
@@ -98,7 +93,15 @@ class InstrumentationManager(
9893
return@addLoggerProviderCustomizer if (options.disableLogs && options.disableErrorTracking) {
9994
sdkLoggerProviderBuilder
10095
} else {
101-
val processor = createLoggerProcessor(sdkLoggerProviderBuilder, customSampler, sdkKey, resources, logger, options)
96+
val processor = createLoggerProcessor(
97+
sdkLoggerProviderBuilder,
98+
customSampler,
99+
sdkKey,
100+
resources,
101+
logger,
102+
telemetryInspector,
103+
options
104+
)
102105
logProcessor = processor
103106
sdkLoggerProviderBuilder.addLogRecordProcessor(processor)
104107
}
@@ -123,8 +126,6 @@ class InstrumentationManager(
123126
}
124127

125128
otelRUM = builder.build()
126-
127-
initializeTelemetryInspector()
128129
loadSamplingConfigAsync()
129130

130131
otelMeter = otelRUM.openTelemetry.meterProvider.get(INSTRUMENTATION_SCOPE_NAME)
@@ -198,7 +199,7 @@ class InstrumentationManager(
198199
buildList {
199200
add(primaryExporter)
200201
add(DebugSpanExporter(logger))
201-
add(InMemorySpanExporter.create().also { inMemorySpanExporter = it })
202+
telemetryInspector?.let { add(it.spanExporter) }
202203
}
203204
)
204205
} else {
@@ -220,7 +221,7 @@ class InstrumentationManager(
220221
buildList {
221222
add(primaryExporter)
222223
add(DebugMetricExporter(logger))
223-
add(InMemoryMetricExporter.create().also { inMemoryMetricExporter = it })
224+
telemetryInspector?.let { add(it.metricExporter) }
224225
}
225226
)
226227
} else {
@@ -237,7 +238,7 @@ class InstrumentationManager(
237238

238239
private fun initializeTelemetryInspector() {
239240
if (options.debug) {
240-
telemetryInspector = TelemetryInspector(inMemorySpanExporter, inMemoryLogExporter, inMemoryMetricExporter)
241+
telemetryInspector = TelemetryInspector()
241242
}
242243
}
243244

@@ -390,20 +391,28 @@ class InstrumentationManager(
390391
sdkKey: String,
391392
resource: Resource,
392393
logger: LDLogger,
394+
telemetryInspector: TelemetryInspector?,
393395
options: Options,
394396
): LogRecordProcessor {
395397
val primaryLogExporter = createOtlpLogExporter(options)
396398
sdkLoggerProviderBuilder.setResource(resource)
397399

398-
val finalExporter = createLogExporter(primaryLogExporter, exportSampler, logger, options)
400+
val finalExporter = createLogExporter(
401+
primaryLogExporter,
402+
exportSampler,
403+
logger,
404+
telemetryInspector,
405+
options
406+
)
399407
val baseProcessor = createBatchLogRecordProcessor(finalExporter)
400408

401409
// Here we set up a routing log processor that will route logs with a matching scope name to the
402410
// respective instrumentation's log record processor. If the log's scope name does not match
403411
// an instrumentation's scope name, it will fall through to the base processor. This was
404412
// originally added to route replay instrumentation logs through a separate log processing
405413
// pipeline to provide instrumentation specific caching and export.
406-
val routingLogRecordProcessor = RoutingLogRecordProcessor(fallthroughProcessor = baseProcessor)
414+
val routingLogRecordProcessor =
415+
RoutingLogRecordProcessor(fallthroughProcessor = baseProcessor)
407416
for (i in options.instrumentations) {
408417
i.getLogRecordProcessor(credential = sdkKey)?.let {
409418
i.getLoggerScopeName().let { scopeName ->
@@ -422,13 +431,19 @@ class InstrumentationManager(
422431
.build()
423432
}
424433

425-
private fun createLogExporter(primaryExporter: LogRecordExporter, exportSampler: ExportSampler, logger: LDLogger, options: Options): LogRecordExporter {
434+
private fun createLogExporter(
435+
primaryExporter: LogRecordExporter,
436+
exportSampler: ExportSampler,
437+
logger: LDLogger,
438+
telemetryInspector: TelemetryInspector?,
439+
options: Options
440+
): LogRecordExporter {
426441
val baseExporter = if (options.debug) {
427442
LogRecordExporter.composite(
428443
buildList {
429444
add(primaryExporter)
430445
add(DebugLogExporter(logger))
431-
// add(InMemoryLogRecordExporter.create().also { inMemoryLogExporter = it }) // TODO: figure out how to factor this out so functions can be static
446+
telemetryInspector?.let { add(it.logExporter) }
432447
}
433448
)
434449
} else {

sdk/@launchdarkly/observability-android/lib/src/main/kotlin/com/launchdarkly/observability/client/TelemetryInspector.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter
1212
* @param metricExporter The in-memory metric exporter to read from
1313
*/
1414
class TelemetryInspector(
15-
val spanExporter: InMemorySpanExporter?,
16-
val logExporter: InMemoryLogRecordExporter?,
17-
val metricExporter: InMemoryMetricExporter?
18-
)
15+
) {
16+
val spanExporter: InMemorySpanExporter by lazy { InMemorySpanExporter.create() }
17+
val logExporter:InMemoryLogRecordExporter by lazy { InMemoryLogRecordExporter.create() }
18+
val metricExporter: InMemoryMetricExporter by lazy { InMemoryMetricExporter.create() }
19+
}

sdk/@launchdarkly/observability-android/lib/src/test/kotlin/com/launchdarkly/observability/client/InstrumentationManagerTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class InstrumentationManagerTest {
6363
testSdkKey,
6464
testResource,
6565
mockLogger,
66+
null,
6667
testOptions
6768
)
6869

@@ -97,6 +98,7 @@ class InstrumentationManagerTest {
9798
testSdkKey,
9899
testResource,
99100
mockLogger,
101+
null,
100102
testOptions
101103
)
102104

@@ -124,6 +126,7 @@ class InstrumentationManagerTest {
124126
testSdkKey,
125127
testResource,
126128
mockLogger,
129+
null,
127130
testOptions
128131
)
129132

0 commit comments

Comments
 (0)