Skip to content

feat: allow attach span context to logs#460

Merged
abelonogov-ld merged 5 commits intomainfrom
andrey/android-span-context
Apr 8, 2026
Merged

feat: allow attach span context to logs#460
abelonogov-ld merged 5 commits intomainfrom
andrey/android-span-context

Conversation

@abelonogov-ld
Copy link
Copy Markdown
Contributor

@abelonogov-ld abelonogov-ld commented Apr 7, 2026

Summary

Adds optional SpanContext parameter to recordLog so log records can be correlated with traces even when OTel context propagation is lost (e.g. detached threads, cross-process boundaries, .NET MAUI bridge).

Changes

SDK (observability-android)

  • Split Observe into MetricsApi, LogsApi, TracesApiObserve now composes all three via interface inheritance. This enables bridge layers to depend on narrower contracts.
  • LogsApi.recordLog gains spanContext: SpanContext? — the old 3-arg signature is preserved as an extension function for backward compatibility.
  • LogRecordEmitter — new shared extension Logger.emitLog(...) that attaches SpanContext to OTel log records via Context.root().with(Span.wrap(...)). Used by both ObservabilityService and KotlinLogger.
  • KotlinLogger — new bridge-layer wrapper that accepts raw traceId/spanId strings (for .NET MAUI interop), reconstructs SpanContext, and routes through either the internal OTel logger or the level-gated LogsApi.
  • LDObserveBridge.getKotlinLogger() — exposes KotlinLogger to bridge consumers.
  • ObservabilityService.getLogger() — exposes the raw OTel Logger instance.

E2E app

  • ViewModel.triggerLogWithContext() — demonstrates capturing SpanContext on the originating thread, then emitting a log from a detached Thread where automatic context propagation is lost.
  • MainActivity — adds "Log with Context" button next to "Trigger Log".

Note

Medium Risk
Changes the recordLog API surface to accept an optional SpanContext, which can impact downstream implementers/callers and affects trace-log correlation behavior. New bridge logger paths and context-setting logic could introduce subtle correlation or gating regressions if misused.

Overview
Adds optional trace correlation to logs by extending recordLog to accept a SpanContext? and updating LDObserve/ObservabilityService to propagate it.

Introduces shared OTel log emission helper (emitLog) plus a new bridge-facing KotlinLogger exposed via LDObserveBridge.getKotlinLogger() to allow non-Kotlin layers to record logs with explicit trace/span IDs. The e2e app UI and ViewModel add a demo action that records a log from a detached thread using a captured span context.

Reviewed by Cursor Bugbot for commit 784b97b. Bugbot is set up for automated code reviews on this repo. Configure here.

@abelonogov-ld abelonogov-ld requested a review from a team as a code owner April 7, 2026 21:04
@abelonogov-ld abelonogov-ld changed the title feagt: allow attach span context to logs feat: allow attach span context to logs Apr 7, 2026
- Introduced `triggerLogWithContext` method in ViewModel to log messages with OpenTelemetry span context.
- Added a button in MainActivity to trigger the new logging functionality, enhancing observability in detached threads.
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit f55dec8. Configure here.

- Cleaned up imports in `LDObserve.kt`, `ObservabilityHookExporter.kt`, `ViewModel.kt`, `ObservabilityServiceTasks.kt`, `SamplingE2ETest.kt`, and `DisablingConfigOptionsE2ETest.kt` by removing the unused `recordLog` import.
- Updated `recordLog` method signature in `Observe.kt` to provide default values for attributes and spanContext, simplifying its usage.
@abelonogov-ld abelonogov-ld enabled auto-merge (squash) April 8, 2026 00:46
@abelonogov-ld abelonogov-ld merged commit 19ae071 into main Apr 8, 2026
24 checks passed
@abelonogov-ld abelonogov-ld deleted the andrey/android-span-context branch April 8, 2026 00:49
abelonogov-ld added a commit that referenced this pull request Apr 8, 2026
* main:
  feat: allow attach span context to logs (#460)
abelonogov-ld pushed a commit that referenced this pull request Apr 8, 2026
🤖 I have created a release *beep* *boop*
---


<details><summary>launchdarkly-observability-android: 0.36.0</summary>

##
[0.36.0](launchdarkly-observability-android-0.35.0...launchdarkly-observability-android-0.36.0)
(2026-04-08)


### Features

* allow attach span context to logs
([#460](#460))
([19ae071](19ae071))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Low risk release bookkeeping only: version bumps and changelog updates
with no functional code changes in this diff.
> 
> **Overview**
> Bumps `sdk/@launchdarkly/observability-android` from `0.35.0` to
`0.36.0` in `.release-please-manifest.json` and the Android
`gradle.properties`.
> 
> Updates `CHANGELOG.md` with the `0.36.0` release entry (noting the new
feature to attach span context to logs).
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
410758e. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants