-
Notifications
You must be signed in to change notification settings - Fork 164
✨ [RUM-8780] Make developer extension capable of injecting the browser SDK #3877
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
Open
BeltranBulbarellaDD
wants to merge
9
commits into
main
Choose a base branch
from
beltran.bulbarella/RUM-8780-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3573f90
Add CDN injection for RUM and LOGS
BeltranBulbarellaDD 4b6ee32
improved badgeStatus
BeltranBulbarellaDD 19b2379
Merge branch 'main' into beltran.bulbarella/RUM-8780-2
BeltranBulbarellaDD f5f25cf
Add allowedTrackingOrigins
BeltranBulbarellaDD cee62a6
Merge branch 'main' into beltran.bulbarella/RUM-8780-2
BeltranBulbarellaDD 0252280
removed app id and client token
BeltranBulbarellaDD 7f5c5da
reset inj variant when mode is disabled
BeltranBulbarellaDD fb2fd7e
reset injection variant when mode is disabled
BeltranBulbarellaDD 5e9f9ce
format and schemas
BeltranBulbarellaDD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import type { Settings } from '../common/extension.types' | ||
| import { EventListeners } from '../common/eventListeners' | ||
| import { DEV_LOGS_URL, DEV_RUM_SLIM_URL, DEV_RUM_URL } from '../common/packagesUrlConstants' | ||
| import { CDN_BASE_URL, CDN_VERSION, DEV_LOGS_URL, DEV_RUM_SLIM_URL, DEV_RUM_URL } from '../common/packagesUrlConstants' | ||
| import { SESSION_STORAGE_SETTINGS_KEY } from '../common/sessionKeyConstant' | ||
| import { createLogger } from '../common/logger' | ||
|
|
||
| declare global { | ||
| interface Window extends EventTarget { | ||
|
|
@@ -11,6 +12,8 @@ declare global { | |
| } | ||
| } | ||
|
|
||
| const logger = createLogger('content-script-main') | ||
|
|
||
| interface SdkPublicApi { | ||
| [key: string]: (...args: any[]) => unknown | ||
| } | ||
|
|
@@ -22,7 +25,6 @@ function main() { | |
| } | ||
|
|
||
| sendEventsToExtension() | ||
|
|
||
| const settings = getSettings() | ||
|
|
||
| if ( | ||
|
|
@@ -47,9 +49,11 @@ function main() { | |
| overrideInitConfiguration(ddLogsGlobal, settings.logsConfigurationOverride) | ||
| } | ||
|
|
||
| if (settings.useDevBundles === 'npm') { | ||
| if (settings.injectionVariant === 'local-dev' && settings.useDevBundles === 'npm') { | ||
| injectDevBundle(settings.useRumSlim ? DEV_RUM_SLIM_URL : DEV_RUM_URL, ddRumGlobal) | ||
| injectDevBundle(DEV_LOGS_URL, ddLogsGlobal) | ||
| } else if (settings.injectionVariant === 'cdn' && settings.datadogMode) { | ||
| injectCdnBundle(settings) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -126,8 +130,7 @@ function loadSdkScriptFromURL(url: string) { | |
| xhr.open('GET', url, false) // `false` makes the request synchronous | ||
| xhr.send() | ||
| } catch (error) { | ||
| // eslint-disable-next-line no-console | ||
| console.error(`[DD Browser SDK extension] Error while loading ${url}:`, error) | ||
| logger.error(`Error while loading ${url}:`, error) | ||
| return | ||
| } | ||
| if (xhr.status === 200) { | ||
|
|
@@ -184,3 +187,109 @@ function instrumentGlobal(global: 'DD_RUM' | 'DD_LOGS') { | |
| function proxySdk(target: SdkPublicApi, root: SdkPublicApi) { | ||
| Object.assign(target, root) | ||
| } | ||
|
|
||
| function injectCdnBundle(settings: Settings) { | ||
| const injectWhenReady = () => { | ||
| if (settings.sdkInjectionType === 'RUM' || settings.sdkInjectionType === 'BOTH') { | ||
| const rumSite = (settings.rumConfigurationOverride as any)?.site as string | undefined | ||
| const rumUrl = getRumBundleUrl(settings.useRumSlim ? 'rum-slim' : 'rum', rumSite) | ||
| const rumConfig = | ||
| settings.rumConfigurationOverride || | ||
| (settings.datadogMode | ||
| ? { | ||
| applicationId: 'xxx', | ||
| clientToken: 'xxx', | ||
| site: 'datad0g.com', | ||
| allowedTrackingOrigins: [location.origin], | ||
| sessionReplaySampleRate: 100, | ||
| } | ||
BeltranBulbarellaDD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| : null) | ||
| injectAndInitializeSDK(rumUrl, 'DD_RUM', rumConfig as any) | ||
| } | ||
|
|
||
| if (settings.sdkInjectionType === 'LOGS' || settings.sdkInjectionType === 'BOTH') { | ||
| const logsSite = (settings.logsConfigurationOverride as any)?.site as string | undefined | ||
| const logsUrl = getLogsBundleUrl(logsSite) | ||
| const logsConfig = | ||
| settings.logsConfigurationOverride || | ||
| (settings.datadogMode | ||
| ? { | ||
| clientToken: 'xxx', | ||
| site: 'datad0g.com', | ||
| allowedTrackingOrigins: [location.origin], | ||
| } | ||
| : null) | ||
| injectAndInitializeSDK(logsUrl, 'DD_LOGS', logsConfig as any) | ||
| } | ||
| } | ||
|
|
||
| if (document.readyState === 'loading') { | ||
| document.addEventListener('DOMContentLoaded', injectWhenReady, { once: true }) | ||
| } else { | ||
| injectWhenReady() | ||
| } | ||
| } | ||
|
|
||
| function getRumBundleUrl(bundle: 'rum' | 'rum-slim', site?: string): string { | ||
| const region = getCdnRegion(site) | ||
| return `${CDN_BASE_URL}/${region}/${CDN_VERSION}/datadog-${bundle}.js` | ||
| } | ||
|
|
||
| function getLogsBundleUrl(site?: string) { | ||
| const region = getCdnRegion(site) | ||
| return `${CDN_BASE_URL}/${region}/${CDN_VERSION}/datadog-logs.js` | ||
| } | ||
BeltranBulbarellaDD marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per Benoit's good suggestion. This will be removed and use US1 prod. |
||
|
|
||
| function getCdnRegion(site?: string) { | ||
| if (!site || site === 'datadoghq.com') { | ||
| return 'us1' | ||
| } | ||
| if (site === 'datadoghq.eu') { | ||
| return 'eu1' | ||
| } | ||
| if (site?.startsWith('us3.')) { | ||
| return 'us3' | ||
| } | ||
| if (site?.startsWith('us5.')) { | ||
| return 'us5' | ||
| } | ||
| if (site?.endsWith('datad0g.com')) { | ||
| return 'us3' | ||
| } | ||
BeltranBulbarellaDD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return 'us1' | ||
| } | ||
BeltranBulbarellaDD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| function injectAndInitializeSDK(url: string, globalName: 'DD_RUM' | 'DD_LOGS', config: object | null) { | ||
| // If the SDK is already loaded, don't try to load it again | ||
| if (window[globalName]) { | ||
| logger.log(`${globalName} already exists, skipping injection`) | ||
| return | ||
| } | ||
|
|
||
| if (url.includes('datadoghq-browser-agent.com')) { | ||
| const script = document.createElement('script') | ||
| script.src = url | ||
| script.async = true | ||
| script.onload = () => { | ||
| if (config && window[globalName] && 'init' in window[globalName]) { | ||
| try { | ||
| window[globalName].init(config) | ||
| } catch (e) { | ||
| logger.error(`Error initializing ${globalName}:`, e) | ||
| } | ||
| } else { | ||
| logger.log(`${globalName} loaded. No init called (no config provided).`) | ||
| } | ||
| } | ||
| script.onerror = (e) => { | ||
| logger.error(`Error loading ${globalName} script:`, e) | ||
| } | ||
| try { | ||
| document.head.appendChild(script) | ||
| } catch (appendErr) { | ||
| logger.error('failed to append script to head, retrying on documentElement', appendErr) | ||
| document.documentElement.appendChild(script) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ const DEFAULT_SETTINGS: Readonly<Settings> = { | |
| logsConfigurationOverride: null, | ||
| debugMode: false, | ||
| datadogMode: false, | ||
| injectionVariant: 'local-dev', | ||
| sdkInjectionType: 'BOTH', | ||
| } | ||
|
|
||
| let settings: Settings | undefined | ||
|
|
@@ -46,9 +48,19 @@ async function loadSettingsFromStorage() { | |
|
|
||
| function setSetting<Name extends keyof Settings>(name: Name, value: Settings[Name]) { | ||
| settings![name] = value | ||
|
|
||
| const settingsToStore: Partial<Settings> = { [name]: value } | ||
|
|
||
| // Reset injectionVariant to default when Datadog mode is disabled | ||
| if (name === 'datadogMode' && value === false) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not applicable anymore. |
||
| settings!.injectionVariant = DEFAULT_SETTINGS.injectionVariant | ||
| settingsToStore.injectionVariant = DEFAULT_SETTINGS.injectionVariant | ||
| } | ||
|
|
||
| onSettingsChange.notify() | ||
|
|
||
| chrome.storage.local | ||
| .set({ [name]: value }) | ||
| .set(settingsToStore) | ||
| .catch((error) => logger.error('Error while storing setting to the storage', error)) | ||
| if (settings) { | ||
| syncSettingsWithSessionStorage(settings) | ||
|
|
||
Submodule rum-events-format
updated
6 files
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔨 warning: The
settingsshould be redesigned to make it more explicit and ideally avoid forbidden states. Things that should be clarified at the settings level:useDevBundles: npmwon't work ifinjectionVariantis notcdn.datadogMode?injectionVariantandsdkInjectionTypejust by looking at property names?My suggestion: keep things simple. Always inject both RUM and Logs CDN bundles with a default config. If dev bundle override is enabled, those bundles will be overridden (including rum-slim override). If config override is enabled, the config will be overridden. No need to do anything at the injection level.
So in the end you have a single option,
inject: boolean. No need to enforcedatadogModeusage, people can use it it's fine.