Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rum] Inject browser sdk #119

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Conversation

yoannmoinet
Copy link
Member

@yoannmoinet yoannmoinet commented Nov 29, 2024

What and why?

Automatically inject RUM's Browser SDK into the bundle.

How?

A few updates were necessary:

  • Add auth to the doRequest helper, so we can fetch the correct clientToken in case the customer didn't add it to the rum's configuration.
  • Improve logger and build report with more data on the bundler.
  • Create a better isolation of the injected code using an IIFE.
  • Update the bundling mechanism of our plugins to allow side builds declared by the plugins (ex: RUM needs to pre-bundle the browser SDK file into a single file).
  • Implement the RUM plugin and add tests.
    • It also publishes a new file alongside our bundler plugins rum-browser-sdk.js which is used for the injection.

TODO

  • Test RUM's SDK in real life situation.

@yoannmoinet yoannmoinet linked an issue Nov 29, 2024 that may be closed by this pull request
@yoannmoinet yoannmoinet self-assigned this Nov 29, 2024
@yoannmoinet yoannmoinet changed the base branch from master to yoann/move-sourcemaps November 29, 2024 15:32
@yoannmoinet yoannmoinet force-pushed the yoann/inject-browser-sdk branch 2 times, most recently from 47ec98f to 73486c7 Compare December 2, 2024 10:20
@yoannmoinet yoannmoinet force-pushed the yoann/inject-browser-sdk branch from a305acd to fceab29 Compare December 9, 2024 15:29
@yoannmoinet yoannmoinet changed the base branch from yoann/move-sourcemaps to yoann/add-more-injection-points December 9, 2024 15:29
@yoannmoinet yoannmoinet force-pushed the yoann/inject-browser-sdk branch from fceab29 to 3c35484 Compare December 9, 2024 16:35
@yoannmoinet yoannmoinet force-pushed the yoann/add-more-injection-points branch from 84950be to 74d2575 Compare January 14, 2025 14:23
@yoannmoinet yoannmoinet force-pushed the yoann/inject-browser-sdk branch 2 times, most recently from e401db0 to d66990f Compare January 16, 2025 10:05
@yoannmoinet yoannmoinet changed the base branch from yoann/add-more-injection-points to yoann/better-sandbox-tests January 16, 2025 10:05
@yoannmoinet yoannmoinet force-pushed the yoann/inject-browser-sdk branch from d66990f to 5f3ea86 Compare January 16, 2025 13:55
@yoannmoinet yoannmoinet changed the base branch from yoann/better-sandbox-tests to yoann/lighten-readmes January 16, 2025 13:55
Base automatically changed from yoann/lighten-readmes to master February 6, 2025 21:40
@yoannmoinet yoannmoinet changed the base branch from master to yoann/v2.5.0 February 10, 2025 15:08
@yoannmoinet yoannmoinet mentioned this pull request Feb 11, 2025
@yoannmoinet yoannmoinet force-pushed the yoann/inject-browser-sdk branch from 5f3ea86 to f158564 Compare February 11, 2025 16:19
@yoannmoinet yoannmoinet changed the base branch from yoann/v2.5.0 to yoann/add-e2e-testing February 11, 2025 16:19
@yoannmoinet yoannmoinet marked this pull request as ready for review February 12, 2025 08:55
datadogWebpackPlugin({
rum?: {
disabled?: boolean,
sdk?: {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we could use the RUM type directly here, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a very good idea, I'll check if I can.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I put that on the wrong file, oops, this is the README, not the source types

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah nice Idea!

Comment on lines 14 to 16
- [rum.sdk.applicationId](#rumsdkapplicationid)
- [rum.sdk.clientToken](#rumsdkclienttoken)
- [rum.sdk.site](#rumsdksite)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similarly, maybe we don't need to document all of those. Instead we could reference the general doc in the public documentation website, WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense, I'm afraid of getting out of sync, but I do prefer having a single source of truth.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If you want only 1 source of truth, you'd need to add a dependency on @datadog/browser-rum instead of checking the global DD_RUM (right now you're only using a devDep)

Comment on lines +14 to +16
DD_RUM.addAction('custom_click', {
bundler: '{{bundler}}',
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

❤️


import { datadogRum } from '@datadog/browser-rum';

// To please TypeScript.
Copy link
Collaborator

Choose a reason for hiding this comment

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

😂

globalAny.DD_RUM = globalAny.DD_RUM || {};
globalAny.DD_RUM = {
...globalAny.DD_RUM,
...datadogRum,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't have any knowledge on whether window.DD_RUM and datadogRum share the exact same API. I'll let @Miz85 review this one

Copy link
Member Author

Choose a reason for hiding this comment

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

From the documentation website:

image

I'd expect the two to be the same.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes they are the same.

But we should choose on strategy for the injection and roll with that, why do we want to support both the CDN and the npm install methods?

Comment on lines +74 to +93
const sdkWithDefault: SDKOptionsWithDefaults = {
applicationId: 'unknown_application_id',
allowUntrustedEvents: false,
compressIntakeRequests: false,
defaultPrivacyLevel: 'mask',
enablePrivacyForActionName: false,
sessionReplaySampleRate: 0,
sessionSampleRate: 100,
silentMultipleInit: false,
site: 'datadoghq.com',
startSessionReplayRecordingManually: false,
storeContextsAcrossPages: false,
telemetrySampleRate: 20,
traceSampleRate: 100,
trackingConsent: 'granted',
trackLongTasks: false,
trackResources: false,
trackUserInteractions: false,
trackViewsManually: false,
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here: Idk if we should re-code the SDK defaults. Is it possible to get them from the SDK package itself? (I'd prefer to avoid duplicating code, specially if they decide to change their defaults)

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair, I'll look into it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Edit: as you're fully pinning a version of the browser SDK, hard coding values is not needed (as you can decide to update those when updating the underlying RUM version)

Idk if you want to manually check that the default values are the same, so it could still be good, but it's not required

Copy link
Collaborator

@Ayc0 Ayc0 left a comment

Choose a reason for hiding this comment

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

Nice!

// Also them to the global DD_RUM object.
globalAny.DD_RUM = globalAny.DD_RUM || {};
globalAny.DD_RUM = {
...globalAny.DD_RUM,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Now that I better understand the fact that we are using a pre-bundled version of the SDK, does it make sense to try to inject here both ...DD_RUM, ...datadogRum? (As those 2 could be using different versions)

Why not just doing globalAny.DD_RUM = datadogRum? (also IIRC, even the npm datadogRum should write in DD_RUM 🤔)

telemetrySampleRate?: number;
traceSampleRate?: number;
trackingConsent?: 'granted' | 'not_granted';
trackLongTasks?: boolean;
Copy link
Collaborator

Choose a reason for hiding this comment

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

A few of these options were removed from the browser sdk config in v6. I guess this works still as it will work for v5 and prior and just do nothing for v6+ but just a heads up.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you know which ones? Because they all are still in the doc.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah you're right, they're still there. They just default to true so we removed them from the default config. You can keep them!

datadogWebpackPlugin({
rum?: {
disabled?: boolean,
sdk?: {
Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah nice Idea!

globalAny.DD_RUM = globalAny.DD_RUM || {};
globalAny.DD_RUM = {
...globalAny.DD_RUM,
...datadogRum,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes they are the same.

But we should choose on strategy for the injection and roll with that, why do we want to support both the CDN and the npm install methods?

Copy link
Member

@elbywan elbywan left a comment

Choose a reason for hiding this comment

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

lgtm

Base automatically changed from yoann/add-e2e-testing to yoann/v2.5.0 February 13, 2025 10:50
@yoannmoinet yoannmoinet requested a review from a team as a code owner February 13, 2025 10:58
@yoannmoinet yoannmoinet requested review from jakub-g and removed request for a team February 13, 2025 10:58
@yoannmoinet yoannmoinet force-pushed the yoann/inject-browser-sdk branch 2 times, most recently from 68f129f to 378742a Compare February 13, 2025 14:05
Base automatically changed from yoann/v2.5.0 to master February 13, 2025 14:32
@yoannmoinet yoannmoinet force-pushed the yoann/inject-browser-sdk branch from 378742a to d8d6d43 Compare February 17, 2025 08:47
@jakub-g jakub-g removed their request for review February 18, 2025 09:16
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.

Implement RUM's Browser SDK injection
4 participants