Skip to content

feat: Add MessageBus to core, add MakeMessageHandler to Feature#181

Merged
awforsythe merged 9 commits into
mainfrom
aforsythe/RUM-12206/feature-messaging
Mar 24, 2026
Merged

feat: Add MessageBus to core, add MakeMessageHandler to Feature#181
awforsythe merged 9 commits into
mainfrom
aforsythe/RUM-12206/feature-messaging

Conversation

@awforsythe

@awforsythe awforsythe commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

refs: RUM-12206

This PR builds upon #180, hooking the new MessageBus into the Core and providing Feature implementations with the ability to respond to messages.

This change allows a Feature to react to changes in CoreContext by overriding Feature::MakeMessageHandler and providing a callback that will handle ContextChangedMessage.

A feature's message handler function is executed on a new messaging thread, which reads from the Message Bus's queue and fans out each new message to all registered handler functions. This design is consistent with the iOS SDK: messages are processed asynchronously, and handling of messages does not block other threads. It's up to each feature to synchronize access to any state that's shared between the message-handler and other threads.

Out of scope:

  • This PR does not include support for sending messages from a Feature to other features.
  • The current design uses a simple broadcast approach: Features are opted out of message handling by default, but once a feature opts in, it will receive all messages, regardless of type. (Currently, there's only one type.)

Code changes

  • Core now owns a MessageBus and a std::thread that runs MessagingThreadMain
  • CoreContextProvider now keeps a reference to the active MessageBus (valid only while the SDK is running)
  • When any call to FeatureScope::UpdateContext is processed on the context thread, CoreContextProvider broadcasts ContextChangedMessage with an immutable snapshot of the new context value, allowing features to respond on the messaging thread
  • Feature now provides a virtual MakeMessageHandler method, optionally returning a std::function
    • if a feature needs to access or modify its own state in response to messages, it can return a function that captures a weak_ptr to itself
  • On startup, Core:
    • creates a fresh MessageBus
    • calls MakeMessageHandler on all features
    • adds each valid function to the message bus's list of receivers
    • spawns the messaging thread once all features are fully initialized
    • installs the MessageBus into the CoreContextProvider
  • On shutdown, Core tears down messaging state

@datadog-prod-us1-3

datadog-prod-us1-3 Bot commented Mar 11, 2026

Copy link
Copy Markdown

✅ Code Quality    ✅ Code Vulnerabilities    ✅ Library Vulnerabilities    ✅ Secrets

🎉 All green!

🛠️ No new code quality issues
🛡️ No new code vulnerabilities
📚 No new vulnerable libraries detected
🔑 No new secrets detected

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 1a908ed | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback!

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 22d0dc5db9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/datadog/impl/core/core.cpp Outdated
Comment on lines +346 to +350
// Start the messaging thread, which will invoke relevant message handler functions
// for each message sent on the message bus
DATADOG_ASSERT(!_message_bus_thread, "_message_bus_thread already exists on Start()");
_message_bus_thread = std::thread(
MessagingThreadMain, std::ref(_diagnostic_logger), std::ref(*_message_bus)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Defer message thread until all features are started

Core::Start() launches the messaging thread before the OnCoreStarted loop, so a feature that mutates context during Start() (for example Rum::Start() dispatching UpdateContext) can enqueue ContextChangedMessage values that are delivered to handlers of later features before those features have run OnCoreStarted. In that window, handler code can observe partially initialized feature state (including unset _scope) and crash or drop required work. Please activate message delivery only after all features complete startup, or gate handlers until startup is finished.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Startup ordering fixed with ea485aa: all features will receive Start() before the messaging thread is spawned, such that a feature is guaranteed to be fully started before it receives any messages.

@awforsythe

Copy link
Copy Markdown
Contributor Author

/gitlab resync-job-status

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Mar 13, 2026

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-03-13 11:55:07 UTC ℹ️ Start processing command /gitlab resync-job-status


2026-03-13 11:55:22 UTC ℹ️ Devflow: /gitlab resync-job-status

15 jobs status updated and 6 skipped for a total of 21 jobs

@awforsythe

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ea485aa7d2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/datadog/impl/core/feature.hpp Outdated
Comment on lines +141 to +142
* Called by `Core::RegisterFeature()` after the feature has been stored in the
* feature list, at which point `weak_from_this()` is safe to call. If the feature

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Align MakeMessageHandler lifecycle contract with implementation

The new API contract says MakeMessageHandler() is invoked from Core::RegisterFeature(), but Core::Start() now calls it each time the SDK starts (core.cpp startup loop). This mismatch can lead feature authors to implement one-shot handler setup (e.g., moving state or assuming a single call), which breaks or changes behavior on restart when handlers are recreated per run. Please update this contract to reflect per-Start() invocation (or move the call site to registration).

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Indeed, this documentation comment is outdated. Fixed with 1a908ed.

@awforsythe awforsythe marked this pull request as ready for review March 13, 2026 13:29
@awforsythe awforsythe requested a review from a team as a code owner March 13, 2026 13:29
fuzzybinary
fuzzybinary previously approved these changes Mar 19, 2026
Base automatically changed from aforsythe/RUM-12206/message-bus to main March 24, 2026 13:25
@awforsythe awforsythe dismissed fuzzybinary’s stale review March 24, 2026 13:25

The base branch was changed.

@awforsythe awforsythe requested a review from a team as a code owner March 24, 2026 13:25
@awforsythe awforsythe merged commit e2400fb into main Mar 24, 2026
18 checks passed
@awforsythe awforsythe deleted the aforsythe/RUM-12206/feature-messaging branch March 24, 2026 13:25
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