Skip to content

feat(maui): add SetAttributes and RemoveAttributes - #863

Open
abelonogov-ld wants to merge 7 commits into
mainfrom
andrey/global-attributes-maui
Open

abelonogov-ld wants to merge 7 commits into
mainfrom
andrey/global-attributes-maui

Conversation

@abelonogov-ld

@abelonogov-ld abelonogov-ld commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #860 (Android setAttributes). Depends on launchdarkly/swift-launchdarkly-observability#290 (iOS setAttributes). Retarget to main once #860 merges.

Summary

Adds global attributes to the .NET MAUI SDK, matching the other SDKs in this series (#858, #859, #860, #861, #856, launchdarkly/swift-launchdarkly-observability#290):

  • LDObserve.SetAttributes(IDictionary<string, object?>) / LDObserve.RemoveAttributes(IEnumerable<string>) merge into the native SDK's process-wide, in-memory map.
  • Applied to every span, log and metric recorded afterwards, including the native SDK's automatic telemetry. Globals only fill keys the signal doesn't already carry (per-call attributes win). Not persisted; survive session rotation. session.id and highlight.session_id are ignored on both platforms.
  • Safe before init (unlike other LDObserve methods): calls are buffered in a thread-safe C# store and replayed into native right before the native Start, so telemetry recorded during native startup carries them too. After that, calls go straight to native under the same lock, so a racing write can't be overtaken by older buffered values.
  • .NET-side spans (Activity API, TelemetrySpan, System.Net.Http, RecordError) are exported through the native tracer via LDTraceExporter / TraceBuilderAdapter, so native globals apply to them; Activity tags are copied after the native span starts, so they win over globals. Because the native span is created at export, a .NET span gets the globals current when its Activity ends.
  • Native bridge: Kotlin ObservabilityBridge.setAttributes/removeAttributes, Swift @objc static setAttributes: / removeAttributes:, plus the iOS binding ApiDefinition.

Custom span/log processors are intentionally not added for MAUI: telemetry is produced natively, and .NET processors would need a native→.NET callback for every span and log.

How did you test this change?

  • New xUnit project tests/LDObservability.Tests (12 tests) for the pending store: merge/remove/replay ordering, reserved keys, null values, snapshotting, and concurrent writes during bind. dotnet test passes.
  • Built the Android native bridge (./gradlew :LDObserve:assembleRelease), the iOS xcframework (scripts/build_xcframework.sh, Xcode 27.1, against the chore: release main #290 checkout), both bindings, LDObservability for net9.0-android and net9.0-ios, and MauiSample9 for Android and the iOS simulator (the iOS sample needs -p:ValidateXcodeVersion=false locally because .NET for iOS 26.2 expects Xcode 26.2).
  • Not yet verified at runtime on a device or simulator.

Are there any deployment considerations?


Note

Overview
Adds process-wide global attributes to the .NET MAUI observability API via LDObserve.SetAttributes and LDObserve.RemoveAttributes, aligned with the other mobile SDKs. Merged keys attach to subsequent spans, logs, and metrics (including native auto-instrumentation); per-call attributes still override globals. Reserved keys session.id and highlight.session_id are stripped, and null values are ignored.

Unlike other LDObserve calls, pre-init usage is supported: a new GlobalAttributesBuffer holds set/remove operations under a lock and replays them into native immediately before Start via NativeGlobalAttributesSink and new Android Kotlin / iOS Swift bridge methods (plus iOS binding exports). After bind, writes go straight to the native store.

Documentation and the sample app exercise UTM-style globals set before init and per-call override on logs/spans. A new LDObservability.Tests xUnit project links GlobalAttributesBuffer for merge, remove, reserved keys, and concurrent-bind coverage.

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

abelonogov-ld and others added 3 commits October 28, 2026 16:26
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@abelonogov-ld
abelonogov-ld requested a review from a team as a code owner October 28, 2026 23:40
Co-authored-by: Cursor <cursoragent@cursor.com>
Base automatically changed from andrey/global-attributes-android to main November 6, 2026 17:50
abelonogov-ld added a commit that referenced this pull request Nov 6, 2026
## Summary

Adds a global attributes API to the Android observability SDK. This is
one of a set of matching PRs across SDKs that share the same semantics
(see #858, #859, #861, #856, #863,
launchdarkly/swift-launchdarkly-observability#290).

- `LDObserve.setAttributes(Attributes)` and
`LDObserve.setAttributes(Map<String, Any?>)` merge attributes by key
into one in-memory, process-wide map;
`LDObserve.removeAttributes(Collection<String>)` removes keys.
- Applied to every span (values captured at span start), every log
record (including crash reports) and every metric data point recorded
after the call, including the launch-time histogram.
- Precedence: per-call attributes > globals > resource. Globals only
fill missing keys and never overwrite. Upstream
`OtelRumConfig.setGlobalAttributes` is intentionally not used, because
its appenders overwrite per-call values. Instead, internal span and log
processors are registered ahead of the SDK's sampling and export
processors, and metric record methods merge globals under per-call
values.
- `session.id` and `highlight.session_id` are ignored (a warning is
logged).
- Not persisted and not cleared on session rotation. Calls made before
the SDK initializes take effect, because the store exists independently
of the SDK instance.
- Reads are a lock-free load of an immutable snapshot; writes rebuild it
under a lock.
- README documents the API with a Kotlin example.

## How did you test this change?

- New JVM unit tests (`GlobalAttributesStoreTest`,
`GlobalAttributesPipelineTest`): merge/remove, re-setting a key with a
different value type, reserved keys, per-call precedence, concurrent
writers, set-before-init through `LDObserve`, globals captured at span
start, and globals reaching exported spans, logs and metric data points
through real OpenTelemetry providers wired like the SDK.
- `./gradlew :lib:compileDebugKotlin :lib:test` passes (559 tests, debug
and release).

## Are there any deployment considerations?

- `ObservabilityOptions` is unchanged. The only API change is three new
methods on `Observe` (inherited by `LDObserve` and its companion), with
default implementations, so Kotlin implementers need no changes.
- Java and MAUI callers use `LDObserve.Companion.setAttributes(...)` /
`removeAttributes(...)`, like the other `LDObserve` methods. #863 (MAUI)
is stacked on this PR.
- Global attributes are added to metric data points, so they increase
metric cardinality.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Overview**
> Adds **process-wide global attributes** on the Android observability
SDK via `LDObserve.setAttributes` (OTel `Attributes` or plain map) and
`LDObserve.removeAttributes`.
> 
> Globals are stored in a singleton **`GlobalAttributesStore`** so calls
**before SDK init** still apply once the pipeline starts. They are
merged into **spans** (snapshot at span start), **logs** (via new
processors registered ahead of sampling/export), and **metrics**
(including launch-time histograms), with **per-call attributes winning**
over globals and reserved session keys ignored with a warning. README
and the compose e2e app document/repro UTM-style globals and precedence.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
7e24d72. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

---------

Co-authored-by: Cursor <cursoragent@cursor.com>

This branch has not been deployed

No deployments
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