-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from DataDog/yoann/inject-browser-sdk
[rum] Inject browser sdk
- Loading branch information
Showing
37 changed files
with
829 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 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 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
Binary file not shown.
Binary file added
BIN
+596 KB
.yarn/cache/@datadog-browser-rum-core-npm-6.0.0-e328c70bce-92ff7c44a4.zip
Binary file not shown.
Binary file not shown.
This file contains 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "@dd/rum-plugin", | ||
"packageManager": "[email protected]", | ||
"license": "MIT", | ||
"private": true, | ||
"author": "Datadog", | ||
"description": "Interact with Real User Monitoring (RUM) directly from your build system.", | ||
"homepage": "https://github.com/DataDog/build-plugins/tree/main/packages/plugins/rum#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/DataDog/build-plugins", | ||
"directory": "packages/plugins/rum" | ||
}, | ||
"toBuild": { | ||
"rum-browser-sdk": { | ||
"entry": "./src/built/rum-browser-sdk.ts" | ||
} | ||
}, | ||
"exports": { | ||
".": "./src/index.ts", | ||
"./*": "./src/*.ts" | ||
}, | ||
"scripts": { | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"@dd/core": "workspace:*", | ||
"chalk": "2.3.1" | ||
}, | ||
"devDependencies": { | ||
"@datadog/browser-rum": "6.0.0" | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2019-Present Datadog, Inc. | ||
|
||
/* global globalThis */ | ||
|
||
import { datadogRum } from '@datadog/browser-rum'; | ||
|
||
// To please TypeScript. | ||
const globalAny: any = globalThis; | ||
|
||
// Also them to the global DD_RUM object. | ||
globalAny.DD_RUM = datadogRum; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2019-Present Datadog, Inc. | ||
|
||
import type { PluginName } from '@dd/core/types'; | ||
|
||
export const CONFIG_KEY = 'rum' as const; | ||
export const PLUGIN_NAME: PluginName = 'datadog-rum-plugin' as const; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2019-Present Datadog, Inc. | ||
|
||
import type { PluginOptions, GetPlugins, GlobalContext } from '@dd/core/types'; | ||
import { InjectPosition } from '@dd/core/types'; | ||
import path from 'path'; | ||
|
||
import { CONFIG_KEY, PLUGIN_NAME } from './constants'; | ||
import { getInjectionValue } from './sdk'; | ||
import type { | ||
OptionsWithRum, | ||
RumOptions, | ||
RumOptionsWithSdk, | ||
RumPublicApi, | ||
RumInitConfiguration, | ||
} from './types'; | ||
import { validateOptions } from './validate'; | ||
|
||
export { CONFIG_KEY, PLUGIN_NAME }; | ||
|
||
export const helpers = { | ||
// Add the helpers you'd like to expose here. | ||
}; | ||
|
||
export type types = { | ||
// Add the types you'd like to expose here. | ||
RumOptions: RumOptions; | ||
OptionsWithRum: OptionsWithRum; | ||
RumPublicApi: RumPublicApi; | ||
RumInitConfiguration: RumInitConfiguration; | ||
}; | ||
|
||
export const getPlugins: GetPlugins<OptionsWithRum> = ( | ||
opts: OptionsWithRum, | ||
context: GlobalContext, | ||
) => { | ||
const log = context.getLogger(PLUGIN_NAME); | ||
const plugins: PluginOptions[] = []; | ||
// Verify configuration. | ||
const options = validateOptions(opts, log); | ||
|
||
// NOTE: These files are built from "@dd/tools/rollupConfig.mjs" and available in the distributed package. | ||
if (options.sdk) { | ||
// Inject the SDK from the CDN. | ||
context.inject({ | ||
type: 'file', | ||
// Using MIDDLE otherwise it's not executed in context. | ||
position: InjectPosition.MIDDLE, | ||
// This file is being built alongside the bundler plugin. | ||
value: path.join(__dirname, './rum-browser-sdk.js'), | ||
}); | ||
|
||
// Inject the SDK Initialization. | ||
context.inject({ | ||
type: 'code', | ||
position: InjectPosition.MIDDLE, | ||
value: getInjectionValue(options as RumOptionsWithSdk, context), | ||
}); | ||
} | ||
|
||
return plugins; | ||
}; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2019-Present Datadog, Inc. | ||
|
||
import { doRequest } from '@dd/core/helpers'; | ||
import type { GlobalContext, InjectedValue } from '@dd/core/types'; | ||
|
||
import type { RumOptionsWithDefaults, RumOptionsWithSdk } from './types'; | ||
|
||
type RumAppResponse = { | ||
data: { | ||
attributes: { | ||
client_token: string; | ||
}; | ||
}; | ||
}; | ||
|
||
const getContent = (opts: RumOptionsWithDefaults) => { | ||
return `DD_RUM.init({${JSON.stringify(opts.sdk).replace(/(^{|}$)/g, '')}}); | ||
`; | ||
}; | ||
|
||
export const getInjectionValue = ( | ||
options: RumOptionsWithSdk, | ||
context: GlobalContext, | ||
): InjectedValue => { | ||
const sdkOpts = options.sdk; | ||
// We already have the clientToken, we can inject it directly. | ||
if (sdkOpts.clientToken) { | ||
return getContent(options); | ||
} | ||
|
||
// Let's try and fetch the clientToken from the API. | ||
if (!context.auth?.apiKey || !context.auth?.appKey) { | ||
throw new Error( | ||
'Missing "auth.apiKey" and/or "auth.appKey" to fetch "rum.sdk.clientToken".', | ||
); | ||
} | ||
|
||
// Return the value as an async function so it gets resolved during buildStart. | ||
return async () => { | ||
let clientToken: string; | ||
try { | ||
// Fetch the client token from the API. | ||
const appResponse = await doRequest<RumAppResponse>({ | ||
url: `https://api.datadoghq.com/api/v2/rum/applications/${sdkOpts.applicationId}`, | ||
type: 'json', | ||
auth: context.auth, | ||
}); | ||
|
||
clientToken = appResponse.data?.attributes?.client_token; | ||
} catch (e: any) { | ||
// Could not fetch the clientToken. | ||
// Let's crash the build. | ||
throw new Error(`Could not fetch the clientToken: ${e.message}`); | ||
} | ||
|
||
// Still no clientToken. | ||
if (!clientToken) { | ||
throw new Error('Missing clientToken in the API response.'); | ||
} | ||
|
||
return getContent({ | ||
...options, | ||
sdk: { | ||
clientToken, | ||
...sdkOpts, | ||
}, | ||
}); | ||
}; | ||
}; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2019-Present Datadog, Inc. | ||
|
||
import type { | ||
datadogRum, | ||
RumInitConfiguration as ExpRumInitConfiguration, | ||
} from '@datadog/browser-rum'; | ||
import type { Assign, GetPluginsOptions } from '@dd/core/types'; | ||
|
||
import type { CONFIG_KEY } from './constants'; | ||
|
||
export type RumOptions = { | ||
disabled?: boolean; | ||
sdk?: SDKOptions; | ||
}; | ||
|
||
export type RumPublicApi = typeof datadogRum; | ||
export type RumInitConfiguration = ExpRumInitConfiguration; | ||
|
||
export type SDKOptions = Assign< | ||
RumInitConfiguration, | ||
{ | ||
// We make clientToken optional because we'll try to fetch it via API if absent. | ||
clientToken?: string; | ||
} | ||
>; | ||
|
||
// Define the SDK options with known defaults. | ||
export type SDKOptionsWithDefaults = Assign< | ||
SDKOptions, | ||
Pick< | ||
Required<SDKOptions>, | ||
| 'applicationId' | ||
| 'allowUntrustedEvents' | ||
| 'compressIntakeRequests' | ||
| 'defaultPrivacyLevel' | ||
| 'enablePrivacyForActionName' | ||
| 'sessionReplaySampleRate' | ||
| 'sessionSampleRate' | ||
| 'silentMultipleInit' | ||
| 'site' | ||
| 'startSessionReplayRecordingManually' | ||
| 'storeContextsAcrossPages' | ||
| 'telemetrySampleRate' | ||
| 'traceSampleRate' | ||
| 'trackingConsent' | ||
| 'trackLongTasks' | ||
| 'trackResources' | ||
| 'trackUserInteractions' | ||
| 'trackViewsManually' | ||
> | ||
>; | ||
|
||
export type RumOptionsWithDefaults = { | ||
disabled?: boolean; | ||
sdk?: SDKOptionsWithDefaults; | ||
}; | ||
|
||
export type RumOptionsWithSdk = Assign<RumOptionsWithDefaults, { sdk: SDKOptionsWithDefaults }>; | ||
|
||
export interface OptionsWithRum extends GetPluginsOptions { | ||
[CONFIG_KEY]: RumOptions; | ||
} |
Oops, something went wrong.