Skip to content

fix: emit context attributes regardless of active span#575

Draft
jbachorik wants to merge 1 commit into
mainfrom
jb/fix-writeCurrentContext-span-gate
Draft

fix: emit context attributes regardless of active span#575
jbachorik wants to merge 1 commit into
mainfrom
jb/fix-writeCurrentContext-span-gate

Conversation

@jbachorik
Copy link
Copy Markdown
Collaborator

What does this PR do?:
Context attributes (e.g. llm.agent.phase) were silently dropped from ExecutionSample events when no span was active. In Recording::writeCurrentContext, ProfiledThread was only fetched when hasContext was true — but hasContext only reflects whether a span is active, not whether thread-local context attributes exist.

Motivation:
ProfiledThread::currentSignalSafe() is a pure TLS lookup: signal-safe, never allocates, returns nullptr if the key is not yet initialized. It is safe to call unconditionally. The existing null-check in the attributes loop (thrd != nullptr ? thrd->getOtelTagEncoding(i) : 0) already handles the no-thread case correctly.

Span IDs continue to be written as zero when no span is active — ContextApi::get only fills them on success, which is unaffected by this change.

Additional Notes:
The old variable hasContext was renamed away (its return value is now discarded) because the name was misleading: LLM phase and other thread-local attributes ARE context, so hasContext = false while attributes are set is a contradiction.

How to test the change?:
AgentPhaseProfilingTest in ddprof-test covers the case: a worker thread sets llm.agent.phase without any active span and verifies that ExecutionSample events carry the value.

For Datadog employees:

  • If this PR touches code that signs or publishes builds or packages, or handles
    credentials of any kind, I've requested a review from @DataDog/security-design-and-guidance.
  • This PR doesn't touch any of that.
  • JIRA: [JIRA-XXXX]

Context attributes (e.g. llm.agent.phase) were silently dropped from
ExecutionSample events when no span was active, because ProfiledThread
was only fetched when hasContext was true.

ProfiledThread::currentSignalSafe() is a pure TLS lookup — signal-safe,
never allocates — and is safe to call unconditionally. Span IDs are
still zeroed when no span is active (ContextApi::get fills them only on
success).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@jbachorik jbachorik added the AI label Jun 3, 2026
@dd-octo-sts
Copy link
Copy Markdown
Contributor

dd-octo-sts Bot commented Jun 3, 2026

CI Test Results

Run: #26891493504 | Commit: 597637d | Duration: 14m 34s (longest job)

All 32 test jobs passed

Status Overview

JDK glibc-aarch64/debug glibc-amd64/debug musl-aarch64/debug musl-amd64/debug
8 - - -
8-ibm - - -
8-j9 - -
8-librca - -
8-orcl - - -
11 - - -
11-j9 - -
11-librca - -
17 - -
17-graal - -
17-j9 - -
17-librca - -
21 - -
21-graal - -
21-librca - -
25 - -
25-graal - -
25-librca - -

Legend: ✅ passed | ❌ failed | ⚪ skipped | 🚫 cancelled

Summary: Total: 32 | Passed: 32 | Failed: 0


Updated: 2026-06-03 14:46:47 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant