Skip to content

feat: forward LD context keys to errors, logs, and custom spans#530

Merged
Vadman97 merged 1 commit into
mainfrom
vkorolik/forward-context-keys-to-errors-logs-spans
May 6, 2026
Merged

feat: forward LD context keys to errors, logs, and custom spans#530
Vadman97 merged 1 commit into
mainfrom
vkorolik/forward-context-keys-to-errors-logs-spans

Conversation

@Vadman97
Copy link
Copy Markdown
Contributor

@Vadman97 Vadman97 commented May 6, 2026

Summary

  • Spread _ldContextKeys (set via LDObserve.setLDContextKeyAttributes) into all OTel data types where it makes semantic sense, not just metrics.
  • startSpan wrapper now applies contextKeys to every span before invoking the caller, so user-created custom spans inherit pathTemplate / owner / account / route attributes automatically. Caller setAttributes calls win on key collisions (OTel merges with last-write-wins).
  • _recordLog spreads contextKeys into the addEvent('log', …) payload so log events carry the same dimensions as their containing span.
  • _recordErrorMessage no longer needs an explicit spread because the startSpan wrapper handles it; comment added at the call site.

Why

Previously _ldContextKeys only flowed into recordCount / recordGauge / recordHistogram / recordUpDownCounter (#510 / commit e49d4812f), so error, log, and custom-span queries could not group by team or route. This meant LD Observability dashboards couldn't replicate Datadog RUM's per-team and per-route error and log breakdowns. After this change, queries like errors group_by context.contextKeys.pathTemplate and logs group_by context.contextKeys.owner produce real values, closing the last big gap relative to the Datadog frontend-errors dashboard.

Coverage matrix

Data type Before After
Counter / Gauge / Histogram / UpDownCounter metric
Click / page-view / user-interaction spans
Errors (recordError / _recordErrorMessage) ✅ (via startSpan wrapper)
Logs (_recordLog addEvent)
Custom spans created via startSpan

Test plan

  • yarn vitest run src/__tests__/sdk.test.ts — 14/14 passing locally
    • new tests cover: error span attrs, log addEvent attrs, custom span attrs, override semantics, no-context-keys no-op
  • yarn turbo run build --filter '@launchdarkly/observability' — clean build
  • yarn format-check — passes
  • Verify in catfood: queries on errors / logs grouped by context.contextKeys.pathTemplate / owner / accountId populate after a gonfalon SDK bump

🤖 Generated with Claude Code


Note

Medium Risk
Touches core tracing/logging/error instrumentation by injecting _ldContextKeys into startSpan and log events, which could change span attributes and override behavior across all spans.

Overview
LD context keys set via setLDContextKeyAttributes are now forwarded beyond metrics: startSpan injects them onto every created span (including user custom spans), and _recordLog includes them in addEvent('log', ...) attributes.

Error recording relies on the startSpan wrapper for context key attachment (instead of explicitly spreading them in _recordErrorMessage), and new Vitest coverage asserts propagation plus last-write-wins override semantics and the no-context-keys no-op case.

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

Spread the _ldContextKeys bag (set via LDObserve.setLDContextKeyAttributes)
into all OTel data types where it makes semantic sense, not just metrics:

- startSpan wrapper now applies contextKeys to every span before invoking
  the caller, so user-created custom spans inherit pathTemplate / owner /
  account / route attributes automatically. Caller setAttributes calls win
  on key collisions because OTel merges with last-write-wins semantics.
- _recordLog spreads contextKeys into the addEvent('log', ...) payload so
  log events carry the same dimensions as their containing span.
- _recordErrorMessage no longer needs an explicit spread because the
  startSpan wrapper handles it; comment added at the call site.

Previously contextKeys only flowed onto recordCount / recordGauge /
recordHistogram / recordUpDownCounter, so error / log / span queries
could not group by team or route. Closes the remaining gap relative to
Datadog RUM dashboards.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Vadman97 Vadman97 requested a review from a team as a code owner May 6, 2026 16:29
@Vadman97 Vadman97 merged commit 5a6d164 into main May 6, 2026
24 checks passed
@Vadman97 Vadman97 deleted the vkorolik/forward-context-keys-to-errors-logs-spans branch May 6, 2026 23:30
Vadman97 pushed a commit that referenced this pull request May 6, 2026
🤖 I have created a release *beep* *boop*
---


<details><summary>observability: 1.1.8</summary>

##
[1.1.8](observability-1.1.7...observability-1.1.8)
(2026-05-06)


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * highlight.run bumped to 10.3.0
</details>

<details><summary>session-replay: 1.1.8</summary>

##
[1.1.8](session-replay-1.1.7...session-replay-1.1.8)
(2026-05-06)


### Dependencies

* The following workspace dependencies were updated
  * dependencies
    * highlight.run bumped to 10.3.0
</details>

<details><summary>highlight.run: 10.3.0</summary>

##
[10.3.0](highlight.run-10.2.0...highlight.run-10.3.0)
(2026-05-06)


### Features

* forward LD context keys to errors, logs, and custom spans
([#530](#530))
([5a6d164](5a6d164))
</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**
> Automated version/changelog updates and a dependency bump; low risk
beyond any behavioral changes introduced by `highlight.run@10.3.0` in
downstream consumers.
> 
> **Overview**
> This release PR bumps `@launchdarkly/observability` and
`@launchdarkly/session-replay` from `1.1.7` to `1.1.8`, and updates the
workspace manifest accordingly.
> 
> It also releases `highlight.run` `10.3.0` (from `10.2.0`) and records
the change in changelogs; the notable behavior change called out is
forwarding LD context keys to errors, logs, and custom spans.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
29d2ca0. 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