Skip to content

Commit

Permalink
Override rum config
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Dec 4, 2023
1 parent 2452101 commit 0f1791e
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 165 deletions.
81 changes: 0 additions & 81 deletions developer-extension/src/background/domain/devBundleInjection.ts

This file was deleted.

1 change: 0 additions & 1 deletion developer-extension/src/background/index.ts
@@ -1,3 +1,2 @@
import './domain/devBundleInjection'
import './domain/messageRelay'
import './domain/syncRules'
2 changes: 2 additions & 0 deletions developer-extension/src/common/constants.ts
Expand Up @@ -25,3 +25,5 @@ export const enum PanelTabs {
}

export const DEFAULT_PANEL_TAB = PanelTabs.Events

export const SESSION_STORAGE_SETTINGS_KEY = '__ddBrowserSdkSettings'
1 change: 0 additions & 1 deletion developer-extension/src/common/types.ts
Expand Up @@ -16,7 +16,6 @@ export type DevtoolsToBackgroundMessage = {
export interface NetRequestRulesOptions {
tabId: number
useDevBundles: boolean
injectDevBundles: boolean
useRumSlim: boolean
blockIntakeRequests: boolean
}
Expand Down
54 changes: 0 additions & 54 deletions developer-extension/src/content-scripts/injectDevBundle.ts

This file was deleted.

137 changes: 127 additions & 10 deletions developer-extension/src/content-scripts/main.ts
@@ -1,11 +1,128 @@
// This script is executed in the "main" execution world, the same world as the webpage. Thus, it
// can define a global callback variable to listen to SDK events.

;(window as any).__ddBrowserSdkExtensionCallback = (message: unknown) => {
// Relays any message to the "isolated" content-script via a custom event.
window.dispatchEvent(
new CustomEvent('__ddBrowserSdkMessage', {
detail: message,
})
)
import { EventListeners } from '../common/eventListeners'
import { DEV_LOGS_URL, DEV_RUM_URL, SESSION_STORAGE_SETTINGS_KEY } from '../common/constants'

declare global {
interface Window extends EventTarget {
DD_RUM?: SdkPublicApi
DD_LOGS?: SdkPublicApi
__ddBrowserSdkExtensionCallback?: (message: unknown) => void
}
}

type SdkPublicApi = { [key: string]: (...args: any[]) => unknown }

function main() {
if (window.__ddBrowserSdkExtensionCallback) {
return
}

sendEventsToExtension()

const stringSettings = sessionStorage.getItem(SESSION_STORAGE_SETTINGS_KEY)
const settings = stringSettings && JSON.parse(stringSettings)

if (settings) {
const ddRumGlobal = instrumentGlobal('DD_RUM')
const ddLogsGlobal = instrumentGlobal('DD_LOGS')

if (settings.rumConfigurationOverride) {
overrideInitConfiguration(ddRumGlobal, settings.rumConfigurationOverride)
}

if (settings.logsConfigurationOverride) {
overrideInitConfiguration(ddLogsGlobal, settings.logsConfigurationOverride)
}

if (settings.injectDevBundles) {
injectDevBundle(DEV_RUM_URL, ddRumGlobal)
injectDevBundle(DEV_LOGS_URL, ddLogsGlobal)
}
}
}

main()

function sendEventsToExtension() {
// This script is executed in the "main" execution world, the same world as the webpage. Thus, it
// can define a global callback variable to listen to SDK events.
window.__ddBrowserSdkExtensionCallback = (message: unknown) => {
// Relays any message to the "isolated" content-script via a custom event.
window.dispatchEvent(
new CustomEvent('__ddBrowserSdkMessage', {
detail: message,
})
)
}
}

function injectDevBundle(url: string, global: GlobalInstrumentation) {
loadSdkScriptFromURL(url)
const devInstance = global.get()

if (devInstance) {
global.onSet((sdkInstance) => proxySdk(sdkInstance, devInstance))
global.returnValue(devInstance)
}
}

function overrideInitConfiguration(global: GlobalInstrumentation, configurationOverride: object) {
global.onSet((sdkInstance) => {
const originalInit = sdkInstance.init
sdkInstance.init = (config: any) => {
originalInit({ ...config, ...configurationOverride })
}
})
}

function loadSdkScriptFromURL(url: string) {
const xhr = new XMLHttpRequest()
try {
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)
return
}
if (xhr.status === 200) {
const script = document.createElement('script')
script.type = 'text/javascript'
script.text = xhr.responseText

document.documentElement.prepend(script)
}
}

type GlobalInstrumentation = ReturnType<typeof instrumentGlobal>
function instrumentGlobal(global: 'DD_RUM' | 'DD_LOGS') {
const eventListeners: EventListeners<SdkPublicApi> = new EventListeners<SdkPublicApi>()
let returnedInstance: SdkPublicApi | undefined
let lastInstance: SdkPublicApi | undefined
Object.defineProperty(window, global, {
set(sdkInstance: SdkPublicApi) {
eventListeners.notify(sdkInstance)
lastInstance = sdkInstance
},
get(): SdkPublicApi | undefined {
return returnedInstance ?? lastInstance
},
})

return {
get: () => window[global],
onSet: (callback: (sdkInstance: SdkPublicApi) => void) => {
eventListeners.subscribe(callback)
},
returnValue: (sdkInstance: SdkPublicApi) => {
returnedInstance = sdkInstance
},
}
}

function proxySdk(target: SdkPublicApi, root: SdkPublicApi) {
for (const key in root) {
if (Object.prototype.hasOwnProperty.call(root, key)) {
target[key] = root[key]
}
}
}
15 changes: 14 additions & 1 deletion developer-extension/src/panel/components/panel.tsx
Expand Up @@ -38,7 +38,16 @@ export function Panel() {
>
<Tabs.List className="dd-privacy-allow">
<Tabs.Tab value={PanelTabs.Events}>Events</Tabs.Tab>
<Tabs.Tab value={PanelTabs.Infos}>
<Tabs.Tab
value={PanelTabs.Infos}
rightSection={
isOverridingInitConfiguration(settings) && (
<Text c="orange" fw="bold" title="Overriding init configuration">
</Text>
)
}
>
<Text>Infos</Text>
</Tabs.Tab>
<Tabs.Tab value={PanelTabs.Replay}>
Expand Down Expand Up @@ -84,3 +93,7 @@ export function Panel() {
function isInterceptingNetworkRequests(settings: Settings) {
return settings.blockIntakeRequests || settings.useDevBundles || settings.injectDevBundles || settings.useRumSlim
}

function isOverridingInitConfiguration(settings: Settings) {
return settings.rumConfigurationOverride || settings.logsConfigurationOverride
}

0 comments on commit 0f1791e

Please sign in to comment.