Commit 5a6d164
feat: forward LD context keys to errors, logs, and custom spans (#530)
## 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
- [x] `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
- [x] `yarn turbo run build --filter '@launchdarkly/observability'` —
clean build
- [x] `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](https://claude.com/claude-code)
<!-- CURSOR_SUMMARY -->
---
> [!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.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
642f16d. 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: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 998f2c5 commit 5a6d164
2 files changed
Lines changed: 158 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
223 | 223 | | |
224 | 224 | | |
225 | 225 | | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
226 | 376 | | |
227 | 377 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
247 | 247 | | |
248 | 248 | | |
249 | 249 | | |
| 250 | + | |
250 | 251 | | |
251 | 252 | | |
252 | 253 | | |
| |||
299 | 300 | | |
300 | 301 | | |
301 | 302 | | |
| 303 | + | |
| 304 | + | |
302 | 305 | | |
303 | 306 | | |
304 | 307 | | |
| |||
451 | 454 | | |
452 | 455 | | |
453 | 456 | | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
454 | 462 | | |
455 | 463 | | |
456 | 464 | | |
| |||
0 commit comments