Skip to content

Conversation

@amortemousque
Copy link
Contributor

@amortemousque amortemousque commented Dec 18, 2025

What and why?

This PR adds source code context injection capabilities to the RUM plugin, enabling customers to identify which microfrontend originates each RUM event in their applications. It focuses on setups with separated bundles loaded via dynamic imports (e.g., module federation) by injecting source code metadata at entry points.

How?

  • New sourceCodeContext option in RUM plugin config that accepts service and version
  • Injects minified, SSR-safe snippet that creates DD_SOURCE_CODE_CONTEXT global variable mapping stack traces to service/version metadata
  • E2E tests validating the RUM event enrichment

@amortemousque amortemousque force-pushed the aymeric/rum-microfrontend branch 2 times, most recently from f38fce4 to 761d68d Compare December 18, 2025 09:28
Copy link
Member

@yoannmoinet yoannmoinet left a comment

Choose a reason for hiding this comment

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

Looks super good.
Thanks for tackling this.

Do you think you could also update ownership of the plugin?

packages/plugins/rum @yoannmoinet

I mostly had small comments.

@BeltranBulbarellaDD
Copy link

Looks good!

@amortemousque amortemousque force-pushed the aymeric/rum-microfrontend branch from 761d68d to da7caff Compare January 28, 2026 14:43
@datadog-datadog-prod-us1

This comment has been minimized.

@amortemousque amortemousque force-pushed the aymeric/rum-microfrontend branch from da7caff to 405d4b4 Compare January 29, 2026 14:54
@amortemousque amortemousque reopened this Jan 29, 2026
@amortemousque amortemousque marked this pull request as ready for review January 29, 2026 14:55
@amortemousque amortemousque force-pushed the aymeric/rum-microfrontend branch from 70018c9 to 536f84d Compare January 29, 2026 15:38
Copy link
Member

@yoannmoinet yoannmoinet left a comment

Choose a reason for hiding this comment

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

Added some comments.
Nothing too bad.

<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/svg+xml" sizes="21x21" href="data:image/svg+xml," />
<title>Source Code Context Test</title>
<script src="https://www.datadoghq-browser-agent.com/us1/v6/datadog-rum.js" type="text/javascript"></script>
Copy link
Member

Choose a reason for hiding this comment

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

Is this the Browser SDK?
Not a fan of having a live resource in the tests.
Would the auto injection work instead?

See my other comment about the plugin's setup.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I see. I realize I need to make the architecture clearer 🙂
Here’s my reasoning, happy to challenge it.

  • I want the tests for this plugin to stay isolated and not depend on another plugin. That’s why I didn’t rely on the SDK injection plugin and instead CDN SDK directly. If you prefer I can use the npm SDK setup?
  • For the e2e assertions, I used beforeSend to capture RUM events instead of intercepting network requests like in other tests. My concern is that compression will soon become mandatory for RUM, beforeSend avoids that complexity

Copy link
Member

Choose a reason for hiding this comment

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

I see, the isolation part makes total sense.

Could we maybe use nock to intercept this request, and direct it to the locally installed SDK? The project already has a dependency on it.

It's really only about the network call.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand. I updated the code to use the browser SDK from NPM.
I also removed some tests, following the discussion with Bastien: #255 (comment)

Comment on lines 122 to 130
(globalThis as any).DD_RUM.init({
clientToken: '<CLIENT_TOKEN>',
applicationId: '<APP_ID>',
enableExperimentalFeatures: ['source_code_context'],
beforeSend: (event: any) => {
(globalThis as any).rum_events.push(event);
return true;
},
});
Copy link
Member

Choose a reason for hiding this comment

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

If you do the SDK injection, I guess this will need to be updated as well, since you will most likely pass these options to the plugin configuration (except the beforeSend one).

Can this one be added post initialisation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now, you can pass beforeSend only in the init API. If the injection does not support it, I can’t use it.

Copy link

Choose a reason for hiding this comment

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

If needed, you could probably check the events by using the extension callback __ddBrowserSdkExtensionCallback

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I’d rather avoid a dependency on the internal __ddBrowserSdkExtensionCallback API. It’s more likely to break, even in minor releases. Maybe, it's just me being overcautious 😄

Copy link

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😱

Copy link

@BeltranBulbarellaDD BeltranBulbarellaDD left a comment

Choose a reason for hiding this comment

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

LGTM


export type SourceCodeContextOptions = {
service: string;
version?: string;
Copy link

Choose a reason for hiding this comment

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

❓ question: ‏is there a use case that we want to explicitly support with omitting version?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What if you want to enrich the event with the owner (service) but don’t care about the version or unminification?
Do you think we should enforce the version to prepare customers for the unminification use case?

Comment on lines 122 to 130
(globalThis as any).DD_RUM.init({
clientToken: '<CLIENT_TOKEN>',
applicationId: '<APP_ID>',
enableExperimentalFeatures: ['source_code_context'],
beforeSend: (event: any) => {
(globalThis as any).rum_events.push(event);
return true;
},
});
Copy link

Choose a reason for hiding this comment

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

If needed, you could probably check the events by using the extension callback __ddBrowserSdkExtensionCallback

expect(chunkAction).toMatchObject({ version: SERVICE_VERSION, service: SERVICE_NAME });
});

test('Should enrich RUM fetch resources with source code context (service/version)', async ({
Copy link

Choose a reason for hiding this comment

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

💭 thought: ‏It is great to have integrations tests, not sure if we need to have the whole combinatory of cases though, especially in this repo.
If we add more events with source code context, I'd rather only have to update the browser-sdk repository.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed, I’ll keep only one test on event enrichment and move all combinations to the browser-sdk repo for easier maintenance.

"typecheck": "tsc --noEmit"
},
"dependencies": {
"@datadog/browser-rum": "6.26.0",
Copy link
Member

Choose a reason for hiding this comment

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

Can you use the same version already imported in the repo, or update all the other versions to fit that one.

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.

5 participants