Skip to content

Commit 3b48286

Browse files
committed
feat(android): Add session.id to all metrics
This change ensures that all custom metrics recorded by the Android observability SDK include the `session.id` attribute. It won't work for OTEL instrumentations like io.opentelemetry.okhttp-3.0.
1 parent 967e3f7 commit 3b48286

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,37 +277,37 @@ class InstrumentationManager(
277277
val gauge = gaugeCache.getOrPut(metric.name) {
278278
otelMeter.gaugeBuilder(metric.name).build()
279279
}
280-
gauge.set(metric.value, metric.attributes)
280+
gauge.set(metric.value, metric.attributes.addSessionId())
281281
}
282282

283283
fun recordCount(metric: Metric) {
284284
// TODO: handle double casting to long better
285285
val counter = counterCache.getOrPut(metric.name) {
286286
otelMeter.counterBuilder(metric.name).build()
287287
}
288-
counter.add(metric.value.toLong(), metric.attributes)
288+
counter.add(metric.value.toLong(), metric.attributes.addSessionId())
289289
}
290290

291291
fun recordIncr(metric: Metric) {
292292
val counter = counterCache.getOrPut(metric.name) {
293293
otelMeter.counterBuilder(metric.name).build()
294294
}
295295
// It increments the value until the metric is exported, then it’s reset.
296-
counter.add(1, metric.attributes)
296+
counter.add(1, metric.attributes.addSessionId())
297297
}
298298

299299
fun recordHistogram(metric: Metric) {
300300
val histogram = histogramCache.getOrPut(metric.name) {
301301
otelMeter.histogramBuilder(metric.name).build()
302302
}
303-
histogram.record(metric.value, metric.attributes)
303+
histogram.record(metric.value, metric.attributes.addSessionId())
304304
}
305305

306306
fun recordUpDownCounter(metric: Metric) {
307307
val upDownCounter = upDownCounterCache.getOrPut(metric.name) {
308308
otelMeter.upDownCounterBuilder(metric.name).build()
309309
}
310-
upDownCounter.add(metric.value.toLong(), metric.attributes)
310+
upDownCounter.add(metric.value.toLong(), metric.attributes.addSessionId())
311311
}
312312

313313
fun recordLog(message: String, severity: Severity, attributes: Attributes) {
@@ -385,12 +385,15 @@ class InstrumentationManager(
385385
}
386386
}
387387

388+
private fun Attributes.addSessionId() = this.toBuilder().put(SESSION_ID_ATTRIBUTE, otelRUM.rumSessionId).build()
389+
388390
companion object {
389391
private const val METRICS_PATH = "/v1/metrics"
390392
private const val LOGS_PATH = "/v1/logs"
391393
private const val TRACES_PATH = "/v1/traces"
392394
private const val INSTRUMENTATION_SCOPE_NAME = "com.launchdarkly.observability"
393395
const val ERROR_SPAN_NAME = "highlight.error"
396+
const val SESSION_ID_ATTRIBUTE = "session.id"
394397
private const val BATCH_MAX_QUEUE_SIZE = 100
395398
private const val BATCH_SCHEDULE_DELAY_MS = 1000L
396399
private const val BATCH_EXPORTER_TIMEOUT_MS = 5000L

0 commit comments

Comments
 (0)